forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSTFromPreorder.kt
49 lines (45 loc) · 1.46 KB
/
BSTFromPreorder.kt
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
package questions
import _utils.UseCommentAsDocumentation
import questions.common.TreeNode
import kotlin.test.assertTrue
/**
* Given an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree),
* construct the tree and return its root.
* A preorder traversal of a binary tree displays the value of the node first,
* then traverses `Node.left`, then traverses `Node.right`.
*
* [Source](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/)
*/
@UseCommentAsDocumentation
private fun bstFromPreorder(preorder: IntArray): TreeNode? {
val node = TreeNode(preorder[0])
for (i in 1..preorder.lastIndex) {
addToBST(node, preorder[i])
}
return node
}
private fun addToBST(root: TreeNode, value: Int) {
if (value < root.`val`) {
if (root.left == null) {
root.left = TreeNode(value)
} else {
addToBST(root.left!!, value)
}
} else if (value > root.`val`) {
if (root.right == null) {
root.right = TreeNode(value)
} else {
addToBST(root.right!!, value)
}
}
}
fun main() {
run {
assertTrue {
// https://assets.leetcode.com/uploads/2019/03/06/1266.png
val actual = bstFromPreorder(intArrayOf(8, 5, 1, 7, 10, 12))!!
val expected = TreeNode.from(arrayOf(8, 5, 10, 1, 7, null, 12))!!
actual.isSameAs(expected)
}
}
}