-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructMaximumBinaryTree.java
More file actions
76 lines (70 loc) · 2.13 KB
/
constructMaximumBinaryTree.java
File metadata and controls
76 lines (70 loc) · 2.13 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
/**
* Author : WindAsMe
* File : constructMaximumBinaryTree.java
* Time : Create on 18-8-28
* Location : ../Home/JavaForLeeCode2/constructMaximumBinaryTree.java
* Function :
*/
public class constructMaximumBinaryTree {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
private static TreeNode constructMaximumBinaryTreeResult(int[] nums) {
int max = -1;
int index = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
index = i;
}
}
TreeNode root = new TreeNode(max);
originate(0, index, nums, root, true);
originate(index + 1, nums.length, nums, root, false);
return root;
}
// In recursion to originate
private static void originate(int start, int end, int[] nums, TreeNode node, boolean left) {
if (end <= start)
return;
if (end - start == 1) {
if (left)
node.left = new TreeNode(nums[start]);
else
node.right = new TreeNode(nums[start]);
return;
}
int max = -1;
int index = -1;
for (int i = start; i < end; i++) {
if (nums[i] > max) {
max = nums[i];
index = i;
}
}
if (left) {
node.left = new TreeNode(max);
originate(start, index, nums, node.left, true);
originate(index + 1, end, nums, node.left, false);
}
else {
node.right = new TreeNode(max);
originate(start, index, nums, node.right, true);
originate(index + 1, end, nums, node.right, false);
}
}
private static void dfs(TreeNode node) {
if (node != null) {
System.out.print(node.val + " ");
dfs(node.left);
dfs(node.right);
}
}
public static void main(String[] args) {
int[] tree = {3,2,1,6,0,5};
dfs(constructMaximumBinaryTreeResult(tree));
}
}