Created
June 9, 2021 04:24
-
-
Save tifoaudii/06e6b1fd8e64c247e9aca4ae0742291b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension TreeNode { | |
public func levelOrderTraversal(visit: (TreeNode) -> Void) { | |
visit(self) | |
var queue = [TreeNode]() | |
children.forEach { queue.append($0) } | |
while !queue.isEmpty { | |
let node = queue.removeFirst() | |
visit(node) | |
node.children.forEach { queue.append($0) } | |
} | |
} | |
} | |
let tree = createTree() | |
tree.levelOrderTraversal { | |
print($0.value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment