摘要: 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) 编辑