mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-20 02:26:11 +00:00
Refine kotlin code (#1241)
* style(kotlin): Improve kotlin codes readability. * remove redundant quotes. * style(kotlin): improve codes readability. * style(kotlin): refine kotlin codes. * Create kotlin.yml * Create kotlin.yml * Delete .github/workflows/kotlin * Delete .github/workflows/main.yml * Create kotlin.yml * Update kotlin.yml * Delete .github/workflows/kotlin.yml * Create hello_world_workflow.main.kts * Delete .github/workflows/hello_world_workflow.main.kts * remove empty line
This commit is contained in:
@@ -16,13 +16,14 @@ fun levelOrder(root: TreeNode?): MutableList<Int> {
|
||||
val queue = LinkedList<TreeNode?>()
|
||||
queue.add(root)
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
val list = ArrayList<Int>()
|
||||
while (!queue.isEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?.value!!) // 保存节点值
|
||||
if (node.left != null) queue.offer(node.left) // 左子节点入队
|
||||
|
||||
if (node.right != null) queue.offer(node.right) // 右子节点入队
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?.value!!) // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子节点入队
|
||||
if (node.right != null)
|
||||
queue.offer(node.right) // 右子节点入队
|
||||
}
|
||||
return list
|
||||
}
|
||||
@@ -38,4 +39,4 @@ fun main() {
|
||||
/* 层序遍历 */
|
||||
val list = levelOrder(root)
|
||||
println("\n层序遍历的节点打印序列 = $list")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user