-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path105.construct-binary-tree-from-preorder-and-inorder-traversal.js
96 lines (95 loc) · 2.56 KB
/
105.construct-binary-tree-from-preorder-and-inorder-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
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
90
91
92
93
94
95
96
/*
* @lc app=leetcode id=105 lang=javascript
*
* [105] Construct Binary Tree from Preorder and Inorder Traversal
*
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
*
* algorithms
* Medium (48.58%)
* Likes: 3617
* Dislikes: 98
* Total Accepted: 375.7K
* Total Submissions: 768.9K
* Testcase Example: '[3,9,20,15,7]\n[9,3,15,20,7]'
*
* Given preorder and inorder traversal of a tree, construct the binary tree.
*
* Note:
* You may assume that duplicates do not exist in the tree.
*
* For example, given
*
*
* preorder = [3,9,20,15,7]
* inorder = [9,3,15,20,7]
*
* Return the following binary tree:
*
*
* 3
* / \
* 9 20
* / \
* 15 7
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function (preorder, inorder) {
if (preorder.length === 0) return null;
let root = new TreeNode();
let nodeStack = [],
currentNode = root,
treeNode;
for (let nodeIndex in preorder) {
currentNode.val = preorder[nodeIndex];
// the node is on the left side of current node
if (Number(nodeIndex) + 1 < preorder.length) {
if (
inorder.indexOf(
preorder[Number(nodeIndex) + 1]
) < inorder.indexOf(preorder[nodeIndex])
) {
currentNode.left = new TreeNode();
nodeStack.push(currentNode);
currentNode = currentNode.left;
} else {
// relocate the current node by stack
while (nodeStack.length > 0) {
treeNode = nodeStack.pop();
if (
inorder.indexOf(
preorder[Number(nodeIndex) + 1]
) > inorder.indexOf(treeNode.val)
)
currentNode = treeNode;
else {
nodeStack.push(treeNode);
break;
}
}
currentNode.right = new TreeNode();
currentNode = currentNode.right;
}
}
}
return root;
};
// @lc code=end
// []\n[]
// [1]\n[1]
// [1,2,3]\n[2,3,1]
// [1,2,3]\n[1,2,3]