Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Not...
分类:
其他好文 时间:
2014-06-21 06:40:07
阅读次数:
172
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3]...
分类:
其他好文 时间:
2014-06-20 19:37:40
阅读次数:
136
题目
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
方法
根据树的中序遍历和前序遍历,来构造树,使用递归的思想。
Tre...
分类:
其他好文 时间:
2014-06-20 12:14:49
阅读次数:
262
简单来说,就是二叉树的前序、中序、后序遍历,包括了递归和非递归的方法前序遍历(注释中的为递归版本): 1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 struct TreeNode 9 {1...
分类:
其他好文 时间:
2014-06-17 12:53:42
阅读次数:
416
前中后遍历 递归版 1 /* Recursive solution */ 2 class
Solution { 3 public: 4 vector preorderTraversal(TreeNode *root) { 5 6 vector
resul...
分类:
其他好文 时间:
2014-06-10 19:38:03
阅读次数:
219
第一次看到accept,心里有点小开心/** * Definition for binary tree
* public class TreeNode { * int val; * TreeNode left; * TreeNode right; *
TreeNode...
分类:
其他好文 时间:
2014-06-10 11:53:41
阅读次数:
241
问题:
给定二叉树的前序和中序遍历,重构这课二叉树.
分析:
前序、中序、后序都是针对于根结点而言,所以又叫作先根、中根、后根(当然不是高跟)。
前序:根 左 右
中序:左 根 右
对二叉树,我们将其进行投影,就会发现个有趣的事:
发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系。所...
分类:
其他好文 时间:
2014-06-08 15:20:12
阅读次数:
214
1. 递归解法
2. 非递归解法(空间复杂度O(n)和O(1))...
分类:
其他好文 时间:
2014-06-08 10:47:37
阅读次数:
139
问题:
由中序和后序遍历构造二叉树。
分析:
Construct Binary Tree from Preorder and Inorder
Traversal
//实现
TreeNode *addNode(vector &inorder, int s1, int end1, vector &postorder, int s2, int end2)
{
...
分类:
其他好文 时间:
2014-06-08 09:56:52
阅读次数:
206
Given preorder and inorder traversal of a tree,
construct the binary tree.Note:You may assume that duplicates do not exist in
the tree./** * Definitio...
分类:
其他好文 时间:
2014-05-30 16:02:40
阅读次数:
289