forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1008.java
52 lines (45 loc) · 1.7 KB
/
_1008.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _1008 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252232/JavaC%2B%2BPython-O(N)-Solution
*/
int i = 0;
public TreeNode bstFromPreorder(int[] preorder) {
return bstFromPreorder(preorder, Integer.MAX_VALUE);
}
private TreeNode bstFromPreorder(int[] preorder, int bound) {
if (i == preorder.length || preorder[i] > bound) {
return null;
}
TreeNode root = new TreeNode(preorder[i++]);
root.left = bstFromPreorder(preorder, root.val);
root.right = bstFromPreorder(preorder, bound);
return root;
}
}
public static class Solution2 {
/**
* I'm happy to have come up with this solution completely on my own on 10/13/2021.Enjoy the beauty of recursion!
*/
public TreeNode bstFromPreorder(int[] preorder) {
return bstFromPreorder(preorder, 0, preorder.length);
}
private TreeNode bstFromPreorder(int[] preorder, int start, int end) {
if (start >= end) {
return null;
}
TreeNode root = new TreeNode(preorder[start]);
int i = start + 1;
for (; i < end; i++) {
if (preorder[i] > preorder[start]) {
break;
}
}
root.left = bstFromPreorder(preorder, start + 1, i);
root.right = bstFromPreorder(preorder, i, end);
return root;
}
}
}