Skip to content

Commit 004dcbb

Browse files
committed
Merge branch 'master' of github.com:jackzhenguo/leetcode-csharp
2 parents 02a8235 + 8553fef commit 004dcbb

File tree

2 files changed

+75
-11
lines changed

2 files changed

+75
-11
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
* CSDN Column(http://blog.csdn.net/column/details/14761.html) where detail solutions are.
44
## `Today Update`
55
### Tree
6-
#### 103 Binary Tree Zigzag Level Order Traversal
7-
* [Github:#103 Binary Tree Zigzag Level Order Traversal](/Tree/Tree.TreeLib/ZigzagLevelOrder.cs)
8-
* [CSDN:#103 Binary Tree Zigzag Level Order Traversal](http://blog.csdn.net/daigualu/article/details/72039636)
9-
#### 95 Unique Binary Search Trees II
10-
* [Github:#95 Unique Binary Search Trees II](/Tree/Tree.TreeLib/UniqueBSTSln.cs)
11-
* [CSDN:#95 Unique Binary Search Trees II](http://blog.csdn.net/daigualu/article/details/72051612)
12-
* this is the famous "Catalan number", please reference https://en.wikipedia.org/wiki/Catalan_number
13-
* apply 1: valid stack oepration: ((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))
14-
* apply 2: ![binary trees](/Tree/Tree.TreeLib/binarytrees.jpg)
15-
* apply 3: ![triangles](/Tree/Tree.TreeLib/triangles.jpg)
16-
* apply 4: ![stairs](/Tree/Tree.TreeLib/stairs.jpg)
6+
#### 105 Construct Binary Tree from Preorder and Inorder Traversal
7+
* [Github:#105 Construct Binary Tree from Preorder and Inorder Traversal](/Tree/Tree.TreeLib/BuildTreeByPreAndInorder.cs)
8+
* [CSDN:#105 Construct Binary Tree from Preorder and Inorder Traversal](http://blog.csdn.net/daigualu/article/details/72127022)
9+
* Tips:
10+
* the most important function in solving this issue is
11+
* private TreeNode bulidTree(int preStart, int inStart, int inEnd) ;
12+
* Plus, preStart index in preorder is the root index, which is also the separate point in inorder and it’s left is left subtree and right is right subtree.
1713
---
1814
---
1915

@@ -222,3 +218,14 @@ Tags are following:
222218
* [Github:#572 Subtree of Another Tree](/Tree/Tree.TreeLib/IsSameTreeSln.cs)
223219
* [CSDN:#572 Subtree of Another Tree](http://blog.csdn.net/daigualu/article/details/71908238)
224220
* Subtree of Another Tree: it's extended from problem of "is same tree".
221+
#### 103 Binary Tree Zigzag Level Order Traversal
222+
* [Github:#103 Binary Tree Zigzag Level Order Traversal](/Tree/Tree.TreeLib/ZigzagLevelOrder.cs)
223+
* [CSDN:#103 Binary Tree Zigzag Level Order Traversal](http://blog.csdn.net/daigualu/article/details/72039636)
224+
#### 95 Unique Binary Search Trees II
225+
* [Github:#95 Unique Binary Search Trees II](/Tree/Tree.TreeLib/UniqueBSTSln.cs)
226+
* [CSDN:#95 Unique Binary Search Trees II](http://blog.csdn.net/daigualu/article/details/72051612)
227+
* this is the famous "Catalan number", please reference https://en.wikipedia.org/wiki/Catalan_number
228+
* apply 1: valid stack oepration: ((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))
229+
* apply 2: ![binary trees](/Tree/Tree.TreeLib/binarytrees.jpg)
230+
* apply 3: ![triangles](/Tree/Tree.TreeLib/triangles.jpg)
231+
* apply 4: ![stairs](/Tree/Tree.TreeLib/stairs.jpg)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
// // Given preorder and inorder traversal of a tree, construct the binary tree.
9+
10+
// // Note:
11+
// // You may assume that duplicates do not exist in the tree.
12+
public class Solution
13+
{
14+
private int[] _preorder; //preorder array
15+
private int[] _inorder; //inorder array
16+
public TreeNode BuildTree(int[] preorder, int[] inorder)
17+
{
18+
_preorder = preorder;
19+
_inorder = inorder;
20+
//if(_preorder.Length==0 || inorder.Length==0) return null;
21+
//if(prorder.Length!=inorder.Length) return null;
22+
return bulidTree(0,0,_inorder.Length-1);
23+
}
24+
//preStart: index of root of tree
25+
//inStart: of inorder tree, inStart ~ root index - 1 -> left tree
26+
//EndStart:of inorder tree, root index + 1 ~ inEnd -> right tree
27+
private TreeNode bulidTree(int preStart, int inStart, int inEnd)
28+
{
29+
if (preStart > _preorder.Length - 1 || inStart > inEnd)
30+
{
31+
return null;
32+
}
33+
TreeNode root = new TreeNode(_preorder[preStart]);
34+
// find the index of current root in inorder.
35+
int inIndex = curRootIndex(inStart, inEnd, root);
36+
root.left = bulidTree(preStart + 1, inStart, inIndex - 1);
37+
//right subtree begins position in preorder
38+
preStart += inIndex - inStart + 1;
39+
root.right = bulidTree(preStart, inIndex + 1, inEnd);
40+
return root;
41+
}
42+
43+
private int curRootIndex(int inStart, int inEnd, TreeNode root)
44+
{
45+
for (int i = inStart; i <= inEnd; i++)
46+
{
47+
if (_inorder[i] == root.val)
48+
{
49+
return i;
50+
}
51+
}
52+
return -1;
53+
}
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)