摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?2种解法,一种是正常递归,另一种是使用栈思想 1 #include 2 #include 3 #include 4 using namespace std; 5 struct... 阅读全文
posted @ 2014-03-06 20:20 青轰的后花园 阅读(167) 评论(0) 推荐(0) 编辑
摘要: 跟着Binary Tree Level Order TraversalBinary Tree Level Order Traversal II这两题做下来基本AC没什么难度,题还是连着做顺手。/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/#include #include using namespace... 阅读全文
posted @ 2014-03-06 16:19 青轰的后花园 阅读(133) 评论(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]]思想:链表按层遍历,总体感觉类似多线程在遍历文件时获得文件夹的过程,对于每一层,只获... 阅读全文
posted @ 2014-03-06 15:55 青轰的后花园 阅读(136) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6.思想主要是使用递归,获得节点左枝和右枝的最大值,然后和自己的值相加,如果大于最大值则替换。同时要注意的是:如果该节点有父节点,那么只能返回self.val+left.val或者self.val+right.val.详细参考:http://www.cnbl... 阅读全文
posted @ 2014-03-06 11:02 青轰的后花园 阅读(147) 评论(0) 推荐(0) 编辑