-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.kt
37 lines (32 loc) · 919 Bytes
/
Solution.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
package _107.kotlin
import structure.TreeNode
import java.util.*
/**
* @author relish
* @since 2018/04/26
*
* Definition for a binary tree node.
* class TreeNode(var `val`: Int = 0) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private fun turn(list: ArrayList<ArrayList<Int>>, node: TreeNode?, depth: Int) {
if (node == null) return
if (list.size <= depth) list.add(ArrayList())
list[depth].add(node.`val`)
turn(list, node.left, 1 + depth)
turn(list, node.right, 1 + depth)
}
fun levelOrderBottom(root: TreeNode?): List<List<Int>> {
val list = ArrayList<ArrayList<Int>>()
turn(list, root, 0)
list.reverse()
return list
}
}
fun main(args: Array<String>) {
val list = Solution().levelOrderBottom(TreeNode.createTestData("[3,9,20,null,null,15,7]"))
println(list)
}