Binary Tree Level Order traversal in java

In this post, we will see about Level Order binary tree traversal in java.

Level Order traversal:

Level order traversal of below tree will be:

We will use Queue for Level Order traversal.This algorithm is very similar to Breadth first search of graph.
Steps for Level order traversal algorithm:

  1. Create empty queue and pust root node to it.
  2. Do the following when queue is not empty
    • Pop a node from queue and print it
    • Push left child of popped node to queue if not null
    • Push right child of popped node to queue if not null

Lets say your binary tree is :

So Level Order traversal will work as below:

Lets create java program for level order traversal:

Run above program and you will get following output:

Please go through java interview programs for more such programs.

One Response

  1. Juan C. March 16, 2016

Add Comment