Boundary traversal of binary tree in java

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

In this post, we will see boundary traversal of binary tree in java.

Lets understand boundary traversal of binary tree with example:

If you look closely to above diagram, boundary traversals can be divided into three essential parts

  • Print left edge nodes (Excluding leaf nodes)
  • Print leaf nodes
  • Print right edge nodes (From bottom to top)

Print left edge nodes (Excluding leaf nodes)

If you define boundary for left part, it is nothing but left child if present, otherwise right child.

Print leaf nodes:

Its pretty simple operation.  Whenever you encounter leaf node, print it.

You may also refer print leaf nodes of binary tree.

Print right edge nodes (From bottom to top) :

If you define boundary for right part, it is nothing but right child if present, otherwise left child. As we need to print bottom up, we will print the node while returning back from recursion stack.

Complete java program combining above three parts:

When you run above program, you will get below output:

Java Binary tree tutorial:

Please go through java interview programs for more such programs.

2 Comments

  1. rich December 19, 2016
    • arpitmandliya December 20, 2016

Add Comment