Skip to content

Commit d8c5a9b

Browse files
committed
finish 105,106
1 parent 87bcf02 commit d8c5a9b

2 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 105. Construct Binary Tree from Preorder and Inorder Traversal
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Tree, Depth-first Search.
5+
- Similar Questions: Construct Binary Tree from Inorder and Postorder Traversal.
6+
7+
## Problem
8+
9+
Given preorder and inorder traversal of a tree, construct the binary tree.
10+
11+
**Note:**
12+
You may assume that duplicates do not exist in the tree.
13+
14+
For example, given
15+
16+
```
17+
preorder = [3,9,20,15,7]
18+
inorder = [9,3,15,20,7]
19+
```
20+
21+
Return the following binary tree:
22+
23+
```
24+
3
25+
/ \
26+
9 20
27+
/ \
28+
15 7
29+
```
30+
31+
## Solution
32+
33+
```javascript
34+
/**
35+
* Definition for a binary tree node.
36+
* function TreeNode(val) {
37+
* this.val = val;
38+
* this.left = this.right = null;
39+
* }
40+
*/
41+
/**
42+
* @param {number[]} preorder
43+
* @param {number[]} inorder
44+
* @return {TreeNode}
45+
*/
46+
var buildTree = function(preorder, inorder) {
47+
return helper(preorder, inorder, 0, 0, inorder.length - 1);
48+
};
49+
50+
var helper = function (preorder, inorder, preIndex, inStart, inEnd) {
51+
if (preIndex >= preorder.length || inStart > inEnd) return null;
52+
53+
var index = 0;
54+
var root = new TreeNode(preorder[preIndex]);
55+
56+
for (var i = inStart; i <= inEnd; i++) {
57+
if (inorder[i] === root.val) {
58+
index = i;
59+
break;
60+
}
61+
}
62+
63+
if (index > inStart) root.left = helper(preorder, inorder, preIndex + 1, inStart, index - 1);
64+
if (index < inEnd) root.right = helper(preorder, inorder, preIndex + index - inStart + 1, index + 1, inEnd);
65+
66+
return root;
67+
};
68+
```
69+
70+
**Explain:**
71+
72+
1. `preorder[0]``root`
73+
2.`inorder` 里找到 `root`,左边是 `root.left`,右边是 `root.right`
74+
3. 递归
75+
76+
**Complexity:**
77+
78+
* Time complexity :
79+
* Space complexity :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 106. Construct Binary Tree from Inorder and Postorder Traversal
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Tree, Depth-first Search.
5+
- Similar Questions: Construct Binary Tree from Preorder and Inorder Traversal.
6+
7+
## Problem
8+
9+
Given inorder and postorder traversal of a tree, construct the binary tree.
10+
11+
**Note:**
12+
13+
You may assume that duplicates do not exist in the tree.
14+
15+
For example, given
16+
17+
```
18+
inorder = [9,3,15,20,7]
19+
postorder = [9,15,7,20,3]
20+
```
21+
22+
Return the following binary tree:
23+
24+
```
25+
3
26+
/ \
27+
9 20
28+
/ \
29+
15 7
30+
```
31+
32+
## Solution
33+
34+
```javascript
35+
/**
36+
* Definition for a binary tree node.
37+
* function TreeNode(val) {
38+
* this.val = val;
39+
* this.left = this.right = null;
40+
* }
41+
*/
42+
/**
43+
* @param {number[]} inorder
44+
* @param {number[]} postorder
45+
* @return {TreeNode}
46+
*/
47+
var buildTree = function(inorder, postorder) {
48+
return helper(inorder, postorder, 0, inorder.length - 1, postorder.length - 1);
49+
};
50+
51+
var helper = function (inorder, postorder, inStart, inEnd, postIndex) {
52+
if (inStart > inEnd || postIndex < 0) return null;
53+
54+
var index = 0;
55+
var root = new TreeNode(postorder[postIndex]);
56+
57+
for (var i = inStart; i <= inEnd; i++) {
58+
if (inorder[i] === root.val) {
59+
index = i;
60+
break;
61+
}
62+
}
63+
64+
root.right = helper(inorder, postorder, index + 1, inEnd, postIndex - 1);
65+
root.left = helper(inorder, postorder, inStart, index - 1, postIndex - 1 - (inEnd - index));
66+
67+
return root;
68+
};
69+
```
70+
71+
**Explain:**
72+
73+
see [Construct Binary Tree from Preorder and Inorder Traversal](./construct-binary-tree-from-preorder-and-inorder-traversal.html).
74+
75+
**Complexity:**
76+
77+
* Time complexity : O(n).
78+
* Space complexity : O(n).

0 commit comments

Comments
 (0)