摘要: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode... 阅读全文
posted @ 2013-11-13 11:03 懒猫欣 阅读(237) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivial, could you do it iteratively?/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode ... 阅读全文
posted @ 2013-11-13 10:57 懒猫欣 阅读(212) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to {1,4,2,3}.确定链表长度后将后半段反序再与前半段合并就可以了,记住前后两段一定要断开不然会产生环/** * Definition for singly-linked list. * struct ListNo 阅读全文
posted @ 2013-11-13 10:46 懒猫欣 阅读(155) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space?首先确定是否有环,并找到环上一点,通过该点得到环的长度,用两个差为环长度的指针来探测环的起点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ... 阅读全文
posted @ 2013-11-13 10:14 懒猫欣 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle... 阅读全文
posted @ 2013-11-13 10:00 懒猫欣 阅读(119) 评论(0) 推荐(0) 编辑