2013年11月8日

Construct Binary Tree from Preorder and Inorder Traversal

摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.代码: 1 TreeNode *buildTree(vector &preorder, vector &inorder) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Soluti... 阅读全文

posted @ 2013-11-08 21:54 waruzhi 阅读(117) 评论(0) 推荐(0) 编辑

Flatten Binary Tree to Linked List

摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6代码: 1 void flatten(TreeNod... 阅读全文

posted @ 2013-11-08 21:27 waruzhi 阅读(174) 评论(0) 推荐(0) 编辑

Permutations II

摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].思路:同Permutations。代码: 1 vector > permuteUnique(vector &num) { 2 // IMPORTANT: Please reset any member ... 阅读全文

posted @ 2013-11-08 21:11 waruzhi 阅读(153) 评论(0) 推荐(0) 编辑

Unique Paths II

摘要: Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文

posted @ 2013-11-08 20:48 waruzhi 阅读(138) 评论(0) 推荐(0) 编辑

Path Sum II

摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文

posted @ 2013-11-08 20:32 waruzhi 阅读(128) 评论(0) 推荐(0) 编辑

Unique Binary Search Trees II

摘要: Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文

posted @ 2013-11-08 20:18 waruzhi 阅读(141) 评论(0) 推荐(0) 编辑

Convert Sorted List to Binary Search Tree

摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:递归代码: 1 TreeNode *sortedListToBST(ListNode *head, int len){ 2 if(len == 0){ 3 return NULL; 4 } 5 TreeNode *result = new TreeNode(0); 6 if(le... 阅读全文

posted @ 2013-11-08 19:35 waruzhi 阅读(143) 评论(0) 推荐(0) 编辑

Longest Consecutive Sequence

摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.思路:因为用排序的话就需要nlgn,而题目要求是O(n),所以考虑到用哈希。先 阅读全文

posted @ 2013-11-08 19:15 waruzhi 阅读(165) 评论(0) 推荐(0) 编辑

Binary Tree Postorder Traversal

摘要: Given a binary tree, return thepostordertraversal 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?思路:用一个栈来保存节点。因为需要记录是否访问了左右子节点,所以用一个pair的第一位来记录当前的访问状态。代码: 1 vector postorderTraversa... 阅读全文

posted @ 2013-11-08 18:40 waruzhi 阅读(181) 评论(0) 推荐(0) 编辑

导航