摘要: Q: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]按... 阅读全文
posted @ 2013-06-04 10:24 summer_zhou 阅读(114) 评论(0) 推荐(0) 编辑
摘要: Q:在path sum的基础上,将结果存在vector中。 void dfs(TreeNode *root,int sum,vector<int> &set,vector<vector<int>> &result) { if(!root) return; if(root->val==sum&&!root->left&&!root->right) //leaf { set.push_back(root->val); result.push_bac... 阅读全文
posted @ 2013-06-03 21:40 summer_zhou 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Q: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / ... 阅读全文
posted @ 2013-06-03 21:03 summer_zhou 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Q:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and 阅读全文
posted @ 2013-05-30 18:22 summer_zhou 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Q: Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3... 阅读全文
posted @ 2013-05-30 16:24 summer_zhou 阅读(178) 评论(0) 推荐(0) 编辑
摘要: TreeNode* flat(TreeNode *root) { if(!root) return NULL; TreeNode *tail1 = flat(root->left); TreeNode *tail2 = flat(root->right); if(tail1) { tail1->right = root->right; tail1->left = NULL; root->right = root->left;... 阅读全文
posted @ 2013-05-29 21:34 summer_zhou 阅读(106) 评论(0) 推荐(0) 编辑
摘要: int _maxDepthH(TreeNode *root,int curDepth) { if(!root) return curDepth; curDepth++; if(!root->left&&!root->right) return curDepth; int d1 = _maxDepthH(root->left,curDepth); int d2 = _maxDepthH(root->right,curDepth); return (d1>d2)?d... 阅读全文
posted @ 2013-05-29 17:55 summer_zhou 阅读(117) 评论(0) 推荐(0) 编辑
摘要: void helper(TreeNode *root,unsigned int curDepth,unsigned int *minDepth) { if(curDepth>=*minDepth-1) return; curDepth++; if(!root->left&&!root->right) { *minDepth = curDepth; return; }else { if(root->left) ... 阅读全文
posted @ 2013-05-29 16:37 summer_zhou 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Q:给定一个已排序的数组,将它转换成一个平衡的二叉查找树。A:Divide and conquer。为了达到一个平衡的二叉树,数组的中间元素做为root,然后对数组的左右两个部分分别生成新的子树,分别作为root的左右孩子。时间复杂度:O(n)空间复杂度:无额外开销 TreeNode *buildTree(vector<int> &num,int begin,int end) { if(begin<=end) { int middle = begin + (end - begin)/2; TreeNode *r... 阅读全文
posted @ 2013-05-28 16:58 summer_zhou 阅读(108) 评论(0) 推荐(0) 编辑
摘要: Q:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12 阅读全文
posted @ 2013-05-28 16:34 summer_zhou 阅读(127) 评论(0) 推荐(0) 编辑