File tree Expand file tree Collapse file tree 3 files changed +25
-1
lines changed
construct-binary-search-tree-from-preorder-traversal Expand file tree Collapse file tree 3 files changed +25
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ class Solution {
8
8
}
9
9
}
10
10
11
- fun dfs (root : TreeNode ? , k : Int ): IntArray {
11
+ private fun dfs (root : TreeNode ? , k : Int ): IntArray {
12
12
val ans = IntArray (k + 1 )
13
13
14
14
if (root == null ) {
Original file line number Diff line number Diff line change 86
86
</goals >
87
87
<configuration >
88
88
<sourceDirs >
89
+ <sourceDir >construct-binary-search-tree-from-preorder-traversal</sourceDir >
89
90
<source >er-cha-shu-ran-se-UGC</source >
90
91
<source >utils</source >
91
92
<source >add-two-integers</source >
You can’t perform that action at this time.
0 commit comments