Skip to content

Commit 2e3838a

Browse files
committed
feat: add the solution of Binary Tree Level Order Traversal II(107) with kotlin.
1 parent 676094f commit 2e3838a

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | [][088-kotlin] |
5050
| [100][100-question] | [Same Tree][100-tips] | [][100-java] | | [][100-kotlin] |
5151
| [101][101-question] | [Symmetric Tree][101-tips] | [][101-java] | | [][101-kotlin] |
52-
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [][104-java] | | |
53-
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [][107-java] | | |
52+
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [][104-java] | | [][104-kotlin] |
53+
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [][107-java] | | [][107-kotlin] |
5454
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [][108-java] | | |
5555
| [110][110-question] | [Balanced Binary Tree][110-tips] | [][110-java] | | |
5656
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [][111-java] | | |
@@ -501,4 +501,6 @@
501501
[088-kotlin]: ./src/_088/kotlin/Solution.kt
502502
[100-kotlin]: ./src/_100/kotlin/Solution.kt
503503
[101-kotlin]: ./src/_101/kotlin/Solution.kt
504+
[104-kotlin]: ./src/_104/kotlin/Solution.kt
505+
[107-kotlin]: ./src/_107/kotlin/Solution.kt
504506
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_107/kotlin/Solution.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package _107.kotlin
2+
3+
import structure.TreeNode
4+
import java.util.*
5+
6+
/**
7+
* @author relish
8+
* @since 2018/04/26
9+
*
10+
* Definition for a binary tree node.
11+
* class TreeNode(var `val`: Int = 0) {
12+
* var left: TreeNode? = null
13+
* var right: TreeNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
18+
private fun turn(list: ArrayList<ArrayList<Int>>, node: TreeNode?, depth: Int) {
19+
if (node == null) return
20+
if (list.size <= depth) list.add(ArrayList())
21+
list[depth].add(node.`val`)
22+
turn(list, node.left, 1 + depth)
23+
turn(list, node.right, 1 + depth)
24+
}
25+
26+
fun levelOrderBottom(root: TreeNode?): List<List<Int>> {
27+
val list = ArrayList<ArrayList<Int>>()
28+
turn(list, root, 0)
29+
list.reverse()
30+
return list
31+
}
32+
}
33+
34+
fun main(args: Array<String>) {
35+
val list = Solution().levelOrderBottom(TreeNode.createTestData("[3,9,20,null,null,15,7]"))
36+
println(list)
37+
}

tips/107/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ class Solution {
9898
}
9999
```
100100

101+
kotlin(248ms/100.00%):
102+
```kotlin
103+
class Solution {
104+
105+
private fun turn(list: ArrayList<ArrayList<Int>>, node: TreeNode?, depth: Int) {
106+
if (node == null) return
107+
if (list.size <= depth) list.add(ArrayList())
108+
list[depth].add(node.`val`)
109+
turn(list, node.left, 1 + depth)
110+
turn(list, node.right, 1 + depth)
111+
}
112+
113+
fun levelOrderBottom(root: TreeNode?): List<List<Int>> {
114+
val list = ArrayList<ArrayList<Int>>()
115+
turn(list, root, 0)
116+
list.reverse()
117+
return list
118+
}
119+
}
120+
```
101121

102122
## 结语
103123

0 commit comments

Comments
 (0)