-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructBinaryTreeFromPreorderAndInorderTraversal.js
More file actions
44 lines (37 loc) · 1.46 KB
/
Copy pathconstructBinaryTreeFromPreorderAndInorderTraversal.js
File metadata and controls
44 lines (37 loc) · 1.46 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
// 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}
*/
function getBTree(preorder, preStart, preEnd, inorder, inStart, inEnd, inorderHashMap){
// base cases
let currentNodeValue = preorder[preStart];
let currentNodeIndex = inorderHashMap[currentNodeValue];
let node = new TreeNode(currentNodeValue, null, null);
// console.log(node)
// left side how many nodes
let leftNodeCount = currentNodeIndex - inStart;
// right side how many nodes
let rightNodeCount = inEnd - currentNodeIndex;
if(leftNodeCount >= 1 ) {
node.left = getBTree(preorder, preStart+1, preStart+leftNodeCount, inorder, inStart, currentNodeIndex-1, inorderHashMap);
}
if(rightNodeCount >= 1) {
node.right = getBTree(preorder, preStart+leftNodeCount+1, preStart+leftNodeCount+rightNodeCount, inorder, currentNodeIndex+1, inEnd, inorderHashMap);
}
return node;
}
var buildTree = function(preorder, inorder) {
let inorderHashMap = {};
for(let itr = 0; itr < preorder.length ; itr++) {
inorderHashMap[inorder[itr]] = itr;
}
let root = getBTree(preorder, 0, preorder.length -1, inorder, 0, inorder.length-1, inorderHashMap);
return root;
};