Skip to content

Commit a063d61

Browse files
add 1008
1 parent e11ed55 commit a063d61

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Your ideas/fixes/algorithms are more than welcome!
4242
|1011|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1011.java) | O(nlogn) | O(1) | |Medium|Binary Search|
4343
|1010|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1010.java) | O(n) | O(1) | |Easy|
4444
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
45+
|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1008.java) | O(n) | O(1) | |Medium| Recursion
4546
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | O(n) | O(n) | |Medium|
4647
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
4748
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1008. Construct Binary Search Tree from Preorder Traversal
7+
*
8+
* Return the root node of a binary search tree that matches the given preorder traversal.
9+
* (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)
10+
*
11+
*
12+
* Example 1:
13+
*
14+
* Input: [8,5,1,7,10,12]
15+
* Output: [8,5,10,1,7,null,12]
16+
*
17+
* 8
18+
* / \
19+
* 5 10
20+
* / \ \
21+
* 1 7 12
22+
*
23+
* Note:
24+
*
25+
* 1 <= preorder.length <= 100
26+
* The values of preorder are distinct.
27+
* */
28+
public class _1008 {
29+
public static class Solution1 {
30+
/**credit: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/discuss/252232/JavaC%2B%2BPython-O(N)-Solution*/
31+
int i = 0;
32+
33+
public TreeNode bstFromPreorder(int[] preorder) {
34+
return bstFromPreorder(preorder, Integer.MAX_VALUE);
35+
}
36+
37+
private TreeNode bstFromPreorder(int[] preorder, int bound) {
38+
if (i == preorder.length || preorder[i] > bound) {
39+
return null;
40+
}
41+
TreeNode root = new TreeNode(preorder[i++]);
42+
root.left = bstFromPreorder(preorder, root.val);
43+
root.right = bstFromPreorder(preorder, bound);
44+
return root;
45+
}
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1008;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static junit.framework.Assert.assertEquals;
11+
12+
public class _1008Test {
13+
private static _1008.Solution1 solution1;
14+
private static int[] preorder;
15+
private static TreeNode expected;
16+
private static TreeNode actual;
17+
18+
@Test
19+
public void test1() {
20+
solution1 = new _1008.Solution1();
21+
preorder = new int[]{8, 5, 1, 7, 10, 12};
22+
expected = TreeUtils.constructBinaryTree(Arrays.asList(8, 5, 10, 1, 7, null, 12));
23+
TreeUtils.printBinaryTree(expected);
24+
actual = solution1.bstFromPreorder(preorder);
25+
TreeUtils.printBinaryTree(actual);
26+
assertEquals(expected, actual);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)