-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximumBinaryTree.js
109 lines (88 loc) · 2.78 KB
/
maximumBinaryTree.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
97
98
99
100
101
102
103
104
105
106
107
108
109
class TreeNode {
constructor(val) {
this.val = val;
this.left = this.right = null;
}
}
const constructMaximumBinaryTree = nums => {
if (!nums.length) return null;
const max = Math.max(...nums);
const root = new TreeNode(max);
const index = nums.indexOf(max);
const leftArr = nums.slice(0, index);
const rightArr = nums.slice(index + 1);
const traverse = (node, leftArr, rightArr) => {
if (leftArr.length) {
const leftMax = Math.max(...leftArr);
const leftIndex = leftArr.indexOf(leftMax);
const leftNode = new TreeNode(leftMax);
node.left = leftNode;
const left = leftArr.slice(0, leftIndex);
const right = leftArr.slice(leftIndex + 1);
traverse(node.left, left, right);
}
if (rightArr.length) {
const rightMax = Math.max(...rightArr);
const rightIndex = rightArr.indexOf(rightMax);
const rightNode = new TreeNode(rightMax);
node.right = rightNode;
const left = rightArr.slice(0, rightIndex);
const right = rightArr.slice(rightIndex + 1);
traverse(node.right, left, right);
}
}
traverse(root, leftArr, rightArr);
return root;
};
// const constructMaximumBinaryTree = nums => {
// return construct(nums, 0, nums.length);
// };
// const construct = (nums, l, r) => {
// if(l === r) return null;
// const maxIndex = findMaxIndex(nums, l, r);
// const root = new TreeNode(nums[maxIndex]);
// root.left = construct(nums, l, maxIndex);
// root.right = construct(nums, maxIndex + 1, r);
// return root;
// }
// const findMaxIndex = (nums, left, right) => {
// let maxIndex = left;
// for(let i = left; i < right; i++) {
// if(nums[i] > nums[maxIndex]) {
// maxIndex = i;
// }
// }
// return maxIndex;
// }
// const constructMaximumBinaryTree = nums => {
// if (!nums.length) return null;
// const max = Math.max(...nums);
// const index = nums.indexOf(max);
// const left = nums.slice(0, index);
// const right = nums.slice(index + 1);
// const root = new TreeNode(max);
// root.left = constructMaximumBinaryTree(left);
// root.right = constructMaximumBinaryTree(right);
// return root;
// };
/*
specification
input: integer array with no duplicates
output: maximum binary tree, root is the max number in the array
left subtree is the maximum tree constructed from the left subarray
right subtree is the maximum tree constructed from the right subarray
return root node of tree
constraints:
edge cases:
justification
convert array into maximum binary tree
explanation
the order of the input array will determine the structure of the binary tree
visualization
approximation
find the max number in the array and set it as the root
repeat for rest of tree
verification
implementation
*/
console.log(constructMaximumBinaryTree([3,2,1,6,0,5]));