Print Binary Tree level by level
  • Posted: 4 years ago
  • Updated: 3 years ago
  • Edit
  • answers (1)
  • views (3541)

Design an algorithm to print a binary tree, level by level, starting from the root.

Posted Answers

We can print the binary tree, levelwise, using a \( queue \).

  • Create an empty queue
  • The root of the tree is added in the queue.
  • The queue is removed and printed. If the removed item has any child(ren), add it/them in the queue.
  • Repeat the last step untill the queue is empty.

Running time = \( O(n) \).

public static void printBinaryTreeByLevel(Node node){

Queue queue = new LinkedList ();

while(queue.size() > 0){
Node currentNode = queue.remove();
System.out.println(currentNode.value + );

if(currentNode.left! = null)

if(currentNode.right! = null)


You need to Sign In to post your solution.