-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.kt
73 lines (57 loc) · 1.58 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Stack
class TreeNode(var value: Int = 0) {
var left: TreeNode? = null
var right: TreeNode? = null
}
/*
The binary tree in this example is shown as follow
1
/ \
2 3
/ \ /
4 5 6
/
7
post-order traversal: 4 7 5 2 6 3 1
*/
fun postorderTraversalByRecursion(root: TreeNode?) {
if (root == null) return
postorderTraversalByRecursion(root.left)
postorderTraversalByRecursion(root.right)
print("${root.value} ")
}
fun postorderTraversal(root: TreeNode?) {
if (root == null) return
val nodeStack = Stack<TreeNode>()
nodeStack.push(root)
var pre: TreeNode? = null
while (nodeStack.isNotEmpty()) {
val cur = nodeStack.peek()
if (cur.left != null && cur.left != pre && cur.right != pre) {
nodeStack.push(cur.left)
} else if (cur.right != null && cur.right != pre) {
nodeStack.push(cur.right);
} else {
print("${cur.value} ")
pre = cur
nodeStack.pop()
}
}
}
fun generateBTree(nodes: Array<Int?>, index: Int): TreeNode? {
var node: TreeNode? = null
if (index < nodes.size && nodes[index] != null) {
node = TreeNode(nodes[index]!!)
node.left = generateBTree(nodes, index * 2 + 1)
node.right = generateBTree(nodes, index * 2 + 2)
}
return node
}
fun main(args: Array<String>) {
val nodes = arrayOf(1, 2, 3, 4 ,5, 6, null, null, null, 7)
val root = generateBTree(nodes, 0)
postorderTraversalByRecursion(root)
println()
postorderTraversal(root)
println()
}