- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathMaximumBinaryTree654.java
More file actions
Latest commit
117 lines (103 loc) · 3.28 KB
/
Copy pathMaximumBinaryTree654.java
File metadata and controls
117 lines (103 loc) · 3.28 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Given an integer array with no duplicates. A maximum tree building on this
* array is defined as follow:
*
* The root is the maximum number in the array.
* The left subtree is the maximum tree constructed from left part subarray
* divided by the maximum number.
* The right subtree is the maximum tree constructed from right part subarray
* divided by the maximum number.
*
* Construct the maximum tree by the given array and output the root node of
* this tree.
*
* Example 1:
* Input: [3,2,1,6,0,5]
* Output: return the tree root node representing the following tree:
*
* 6
* / \
* 3 5
* \ /
* 2 0
* \
* 1
* Note:
* The size of the given array will be in the range [1,1000].
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
publicclassMaximumBinaryTree654 {
publicTreeNodeconstructMaximumBinaryTree(int[] nums) {
returnconstructMaximumBinaryTree(nums, 0, nums.length-1);
}
publicTreeNodeconstructMaximumBinaryTree(int[] nums, intlo, inthi) {
if (lo > hi) returnnull;
if (lo == hi) returnnewTreeNode(nums[lo]);
intidx = maxIndex(nums, lo, hi);
TreeNoderoot = newTreeNode(nums[idx]);
root.left = constructMaximumBinaryTree(nums, lo, idx-1);
root.right = constructMaximumBinaryTree(nums, idx+1, hi);
returnroot;
}
privateintmaxIndex(int[] nums, intlo, inthi) {
intres = lo;
intmax = nums[lo];
for (inti=lo; i<=hi; i++) {
if (nums[i] > max) {
max = nums[i];
res = i;
}
}
returnres;
}
publicTreeNodeconstructMaximumBinaryTree2(int[] nums) {
Stack<TreeNode> stack = newStack<>();
for (inti=0; i<nums.length; i++) {
TreeNodecurr = newTreeNode(nums[i]);
if (stack.isEmpty() || stack.peek().val > nums[i]) {
stack.push(curr);
continue;
}
TreeNodeleft = null;
while (!stack.isEmpty() && stack.peek().val < nums[i]) {
TreeNodetemp = stack.pop();
temp.right = left;
left = temp;
}
curr.left = left;
stack.push(curr);
}
TreeNoderes = null;
while (!stack.isEmpty()) {
TreeNodetemp = stack.pop();
temp.right = res;
res = temp;
}
returnres;
}
/**
* https://leetcode.com/problems/maximum-binary-tree/discuss/106156/Java-worst-case-O(N)-solution
*/
publicTreeNodeconstructMaximumBinaryTree3(int[] nums) {
Deque<TreeNode> stack = newLinkedList<>();
for(inti = 0; i < nums.length; i++) {
TreeNodecurr = newTreeNode(nums[i]);
while(!stack.isEmpty() && stack.peek().val < nums[i]) {
curr.left = stack.pop();
}
if(!stack.isEmpty()) {
stack.peek().right = curr;
}
stack.push(curr);
}
returnstack.isEmpty() ? null : stack.removeLast();
}
}