大象放入冰箱几步的思路处理这问题就好了
1.把左子树弄平, 2. 插入到根节点和根节点的右子树之间,3. 重复1,2
思路不难,代码上有点混乱,细节实现是:把root的左子树弄成flatten的 flatten(root.left),把右子树保存起来 temp=root.right,让root.right=root.left, 然后断掉左边,root.left=null.
然后一直走到右边的...
分类:
其他好文 时间:
2014-10-07 06:20:13
阅读次数:
194
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
很简单,先判断第一个位置否为空,不为空的话再判断下 个位置是否为空,如果都为空那么就不用操作了。
如果两...
分类:
其他好文 时间:
2014-10-05 20:16:08
阅读次数:
202
给定一个链表,确定它是否有一个环,不使用额外的空间?...
分类:
其他好文 时间:
2014-10-04 16:08:26
阅读次数:
151
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycl...
分类:
其他好文 时间:
2014-10-04 15:09:06
阅读次数:
187
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* Lis...
分类:
其他好文 时间:
2014-10-03 17:21:45
阅读次数:
167
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ 2 5
/ \ 3 4 6
The flattened tree should look like:
1
...
分类:
其他好文 时间:
2014-10-02 15:12:43
阅读次数:
193
[leetcode]Given a linked list, swap every two adjacent nodes and return its head....
分类:
其他好文 时间:
2014-10-02 10:53:52
阅读次数:
162
Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-o...
分类:
其他好文 时间:
2014-10-01 19:55:31
阅读次数:
122
Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algor...
分类:
其他好文 时间:
2014-10-01 13:46:11
阅读次数:
190
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
这个题目用快慢指针来做,重点在于代码怎么实现的简洁方便理解。
这里用快指针来判断链表是不是有NULL,没有NULL那再继续走,看是否能与慢指针遇上...
分类:
其他好文 时间:
2014-10-01 10:16:10
阅读次数:
309