-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path106 Construct Binary Tree from Inorder and Postorder Traversal.js
More file actions
89 lines (76 loc) · 1.89 KB
/
Copy path106 Construct Binary Tree from Inorder and Postorder Traversal.js
File metadata and controls
89 lines (76 loc) · 1.89 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* 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}
*/
// 76ms faster than ~96.04% and 36MB less than 100%
var buildTree = function(inorder, postorder) {
if (inorder === null || postorder === null) return null;
if (inorder.length !== postorder.length) return null;
return generate(
inorder,
0,
inorder.length - 1,
postorder,
0,
postorder.length - 1
);
};
var generate = function(
inorder,
inLeft,
inRight,
postorder,
postLeft,
postRight
) {
if (inLeft > inRight || postLeft > postRight) return null;
var rootVal = postorder[postRight];
var root = new TreeNode(rootVal);
var rootIndex = search(inorder, inLeft, inRight, rootVal);
if (rootIndex < 0) return null;
var leftTreeSize = rootIndex - inLeft;
var rightTreeSize = inRight - rootIndex;
root.left = generate(
inorder,
inLeft,
rootIndex - 1,
postorder,
postLeft,
postLeft + leftTreeSize - 1
);
root.right = generate(
inorder,
rootIndex + 1,
inRight,
postorder,
postRight - rightTreeSize,
postRight - 1
);
return root;
};
const search = (inOrder, left, right, value) => {
for (var i = left; i <= right; i++) {
if (inOrder[i] === value) return i;
}
return -1;
};
// Alternate DFS solution ~ same performance as prior solution however more concise
var buildTree = function(inorder, postorder) {
return dfs(inorder.length - 1, 0, inorder.length - 1);
function dfs(index, startPos, endPos) {
if (startPos > endPos) return null;
var node = new TreeNode(postorder[index]);
var pos = inorder.indexOf(postorder[index]);
node.left = dfs(index - (endPos - pos) - 1, startPos, pos - 1);
node.right = dfs(index - 1, pos + 1, endPos);
return node;
}
};