Table of Contents [hide]
If you want to practice data structure and algorithm programs, you can go through 100+ java coding 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:
- Binary tree in java
- Binary tree preorder traversal
- Binary tree postorder traversal
- Binary tree inorder traversal
- Binary tree level order traversal
- Binary tree spiral order traversal
- Binary tree reverse level order traversal
- Binary tree boundary traversal
- Print leaf nodes of binary tree
- Count leaf nodes in binary tree
- get maximum element in binary tree
- Print all paths from root to leaf in binary tree
- Print vertical sum of binary tree in java
- Get level of node in binary tree in java
- Lowest common ancestor(LCA) in binary tree in java
Please go through java interview programs for more such programs.
node 45 is wrongly inserted into the tree in example.
Hi Rich,
It is binary tree not a binary search tree, so it does not matter where are you going to insert node 45.