-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0106. Construct Binary Tree from Inorder and Postorder Traversal.js
60 lines (51 loc) · 1.54 KB
/
0106. Construct Binary Tree from Inorder and Postorder Traversal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Given inorder and postorder traversal of a tree, construct the binary tree.
// Note:
// You may assume that duplicates do not exist in the tree.
// For example, given
// inorder = [9, 3, 15, 20, 7]
// postorder = [9, 15, 7, 20, 3]
// Return the following binary tree:
// 3
// / \
// 9 20
// / \
// 15 7
// 1) 递归
// Similar: 0105
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {number[]} inorder
* @param {number[]} postorder
* @return {TreeNode}
*/
const buildTree = (inorder, postorder) => {
if (!inorder.length || !postorder.length) {
return null
}
let rootVal = postorder[postorder.length - 1]
let root = new TreeNode(rootVal)
let index = inorder.indexOf(rootVal)
let leftInorder = inorder.slice(0, index)
let rightInorder = inorder.slice(index + 1)
let leftPostorder = postorder.slice(0, index)
let rightPostorder = postorder.slice(index, postorder.length - 1)
let left = null
let right = null
if (leftInorder.length) {
left = buildTree(leftInorder, leftPostorder)
}
if (rightInorder.length) {
right = buildTree(rightInorder, rightPostorder)
}
root.left = left
root.right = right
return root
}
// Runtime: 112 ms, faster than 47.34 % of JavaScript online submissions for Construct Binary Tree from Inorder and Postorder Traversal.
// Memory Usage: 126.8 MB, less than 100.00 % of JavaScript online submissions for Construct Binary Tree from Inorder and Postorder Traversal.