- LeetCode 102 Binary Tree Level O
- Tree Binary Tree Level Order Tra
- 3.Binary Tree Level Order Traver
- 102. 二叉树的层序遍历
- 107. Binary Tree Level Order Tra
- 2020-07-25 102. Binary Tree Leve
- LeetCode-102-Binary Tree Level O
- 【1错1对0】Binary Tree Level Order T
- 102. Binary Tree Level Order Tra
- leetcode102.Binary Tree Level Or
问题:
方法:
递归遍历,然后按深度存入不同的list,最后输出map的values即为不同层级的nodes。
package com.eric.leetcode
class BinaryTreeLevelOrderTraversalII {
private val result = mutableMapOf<Int, MutableList<Int>>()
fun levelOrderBottom(root: TreeNode?): List<List<Int>> {
result.clear()
root?.let { search(it, 0) }
return result.values.reversed()
}
private fun search(root: TreeNode, depth: Int) {
val list = result[depth]
if (list == null) {
result[depth] = mutableListOf(root.`val`)
} else {
list.add(root.`val`)
}
root.left?.let {
search(it, depth + 1)
}
root.right?.let {
search(it, depth + 1)
}
}
}
有问题随时沟通
网友评论