上一页 1 ··· 16 17 18 19 20
摘要: Q:Given a binary tree, return theinordertraversal of its nodes' values.(1)迭代版本:思路:用stack模仿递归的过程 对于任何结点p 1. 如果p的左孩子不为空,那么将p入栈,p置为左孩子,重复相同的处理 2. 如果p的左孩子为空,则取栈顶元素并进行出栈操作,然后将当前的P置为原栈顶结点的右孩子;重复相同的处理 3. 直至p为NULL或者栈为空,循环结束。 1 vector<int> inorderTraversal(TreeNode *root) { 2 // Start ... 阅读全文
posted @ 2013-05-27 15:53 summer_zhou 阅读(128) 评论(0) 推荐(0)
摘要: double helper(double x, unsigned int n) { if(n==1) return x; double result; if(n%2==0) { result = helper(x,n/2); return result*result; }else{ result = helper(x,(n-1)/2); return result*result*x; } ... 阅读全文
posted @ 2013-05-26 17:17 summer_zhou 阅读(137) 评论(0) 推荐(0)
摘要: //Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]struct QueueNode { TreeNode *pTr; ... 阅读全文
posted @ 2013-05-25 21:17 summer_zhou 阅读(113) 评论(0) 推荐(0)
上一页 1 ··· 16 17 18 19 20