Binary Tree Level Order traversal in java

Previous
Next
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

Example:
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.

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

3 Comments

  1. Juan C. March 16, 2016
  2. Saket Kumar April 20, 2017
  3. Kpd August 3, 2017

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog