Binary Tree Level Order traversal in java

Previous
Next

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.

This is 5th part of java binary tree tutorial.

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

Level Order traversal

Level order traversal of below binary 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:

Level Order traversal of binary tree will be:
40 20 60 10 30 50 70

Java Binary tree tutorial

Please go through java interview programs for more such programs.

Previous
Next

3 Comments

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

Add Comment