-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_173.kt
More file actions
76 lines (67 loc) · 2.09 KB
/
Solution_173.kt
File metadata and controls
76 lines (67 loc) · 2.09 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.zyflool.kotlin
import java.time.LocalDateTime
import java.util.*
import kotlin.collections.ArrayList
import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType
/*
173. 二叉搜索树迭代器
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next() 将返回二叉搜索树中的下一个最小的数。
示例:
BSTIterator iterator = new BSTIterator(root);
iterator.next(); // 返回 3
iterator.next(); // 返回 7
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 9
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 15
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 20
iterator.hasNext(); // 返回 false
提示:
next() 和 hasNext() 操作的时间复杂度是 O(1),并使用 O(h) 内存,其中 h 是树的高度。
你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 中至少存在一个下一个最小的数。
*/
fun main(args: Array<String>) {
var n:Array<TreeNode> = Array(5) {TreeNode(0)}
n[0].`val` = 7
n[1].`val` = 3
n[2].`val` = 15
n[3].`val` = 9
n[4].`val` = 20
n[0].left = n[1]
n[0].right = n[2]
n[2].left = n[3]
n[2].right = n[4]
var iterator = BSTIterator(n[0])
println(iterator.next()) // 返回 3
println(iterator.next()) // 返回 7
println(iterator.hasNext()) // 返回 true
println(iterator.next()) // 返回 9
println(iterator.hasNext()) // 返回 true
println(iterator.next()) // 返回 15
println(iterator.hasNext()) // 返回 true
println(iterator.next()) // 返回 20
println(iterator.hasNext()) // 返回 false
}
class BSTIterator(root: TreeNode?) {
var list: ArrayList<Int> = ArrayList()
var it: Int = 0
init {
LDR(root)
}
fun LDR(root: TreeNode?) {
if(root ==null){
return
}
LDR(root.left)
list.add(root.`val`)
LDR(root.right)
}
fun next(): Int {
return list[it++]
}
fun hasNext(): Boolean {
return it < list.size
}
}