-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructBinarySearchTreeFromPreorderTraversal.java
112 lines (98 loc) · 2.57 KB
/
ConstructBinarySearchTreeFromPreorderTraversal.java
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
// https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/
// #tree
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode
* right; TreeNode(int x) { val = x; } }
*/
/*
BEST WAYS:
helper(preorder, mn, mx, idx[]):
value = preorder[idx[0]]
if value > mx || value < mn:
return
root = new TreeNode(value)
idx[0] += 1
root.left = helper(preorder, mn, value, idx)
root.right = helper(preorder, value, mx, idx)
helper(-INF, INF, 0) // 8
helper(-INF, 8, 1) // 5
helper(-INF, 5, 2) // 1
helper(-INF, 1, 3)
helper(1, 5, 3)
helper(5, 8, 3) // 7
helper(5, 7, 4)
helper(7, 8, 4)
helper(8, INF, 4) // 10
8
/ \
5 10
/ \
1 7
8
/ \
5 10
/ \ \
1 7 12
[mn, mx]
[mn, preorder[l]-1] [preorder[l]+1, mx]
GOOD WAYS:
helper(l, r) -> TreeNode (root)
root = new TreeNode(preorder[l])
// loop
root.left = helper(idx, mn, preorder[l])
root.right = helper(idx, preorder[l], mx)
*/
/* SIMPLE WAY
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
if(preorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[0]);
for(int i = 1; i < preorder.length; i++) {
addToTree(root, preorder[i]);
}
return root;
}
private void addToTree(TreeNode root, int x) {
TreeNode iter = root;
while(iter != null) {
if (iter.val > x) {
if (iter.left == null) {
iter.left = new TreeNode(x);
break;
}
iter = iter.left;
}
else { // iter.val < x
if (iter.right == null) {
iter.right = new TreeNode(x);
break;
}
iter = iter.right;
}
}
}
}
*/
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
if (preorder.length == 0) return null;
int[] idx = {0};
int INF = 1000000007;
TreeNode root = helper(preorder, -INF, INF, idx);
return root;
}
private TreeNode helper(int[] preorder, int mn, int mx, int[] idx) {
if (idx[0] >= preorder.length) {
return null;
}
int value = preorder[idx[0]];
if (value > mx || value < mn) {
return null;
}
TreeNode root = new TreeNode(value);
idx[0] += 1;
root.left = helper(preorder, mn, value, idx);
root.right = helper(preorder, value, mx, idx);
return root;
}
}