Skip to content

Instantly share code, notes, and snippets.

@AndrejJurkin
Created June 9, 2017 09:56
Show Gist options
  • Save AndrejJurkin/0c09e7f9fd297c564452f3271c40c20a to your computer and use it in GitHub Desktop.
Save AndrejJurkin/0c09e7f9fd297c564452f3271c40c20a to your computer and use it in GitHub Desktop.
public void bfs(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()) {
TreeNode node = queue.remove();
System.out.print(node.element + " ");
if(node.left != null) {
queue.add(node.left);
}
if(node.right != null) {
queue.add(node.right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment