Skip to content

Commit 99c6548

Browse files
committed
@ -0,0 +1,23 @@ package com.github.masx200.leetcode_test.construct_binary_search_tree_from_preorder_traversal
1 parent 7be0b55 commit 99c6548

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.masx200.leetcode_test.construct_binary_search_tree_from_preorder_traversal
2+
3+
4+
import com.github.masx200.leetcode_test.insert_into_a_binary_search_tree.TreeNode
5+
6+
class Solution {
7+
fun bstFromPreorder(preorder: IntArray): TreeNode? {
8+
var index = 0
9+
val len = preorder.size
10+
11+
fun dfs(lowerBound: Int, upperBound: Int): TreeNode? {
12+
if (index == len) return null
13+
val cur = preorder[index]
14+
if (cur < lowerBound || cur > upperBound) return null
15+
index++
16+
val root = TreeNode(cur)
17+
root.left = dfs(lowerBound, cur)
18+
root.right = dfs(cur, upperBound)
19+
return root
20+
}
21+
return dfs(Integer.MIN_VALUE, Integer.MAX_VALUE)
22+
}
23+
}

er-cha-shu-ran-se-UGC/index.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Solution {
88
}
99
}
1010

11-
fun dfs(root: TreeNode?, k: Int): IntArray {
11+
private fun dfs(root: TreeNode?, k: Int): IntArray {
1212
val ans = IntArray(k + 1)
1313

1414
if (root == null) {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</goals>
8787
<configuration>
8888
<sourceDirs>
89+
<sourceDir>construct-binary-search-tree-from-preorder-traversal</sourceDir>
8990
<source>er-cha-shu-ran-se-UGC</source>
9091
<source>utils</source>
9192
<source>add-two-integers</source>

0 commit comments

Comments
 (0)