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
| import java.util.LinkedList
class TreeNode(var value: Int) { var left: TreeNode? = null var right: TreeNode? = null }
fun bfs(root: TreeNode?) { val queue: LinkedList<TreeNode> = LinkedList() queue.add(root) while (!queue.isEmpty()) { val node: TreeNode = queue.removeFirst() visit(node)
if (node.left != null) { queue.add(node.left) } if (node.right != null) { queue.add(node.right) } } }
fun visit(root: TreeNode){ println(root.value) }
|