Postorder Traversal Java Program

Binary Tree Postorder Traversal in Java


In Postorder traversal, Left subtree is read first then Right subtree and then Root Node.Postorder traversal of Binary Tree is very popular interview question. There are 2 ways to do Postorder traversal,
1. Recursive Postorder traversal of Binary tree.
2. Iterative Postorder traversal of Binary tree.
   
Postorder traversal example.

Postorder Traversal


There are many ways to traverse a Binary Tree like, Preorder traversal, Inorder traversal, Postorder traversal, Spiral order traversal, Vertical order traversal etc.

In this post, we will focus on Postorder Traversal of Binary Tree

Algorithm: 

Postorder traversal: To traverse a binary tree in Postorder,
1. Traverse the Left subtree.
2. Traverse the Right subtree.
3. Visit the root node and print data of that node

Therefore, the Postorder traversal of the above sample tree will output: 15  35  25  75  45.

Let's see Postorder traversal step by step.

Postorder Traversals Java Program.



class Node{
 private int data;
 private Node left;
 private Node right;

 public Node(int data) {
  this.data=data;
 }

 public int getData() {
  return data;
 }
 public void setData(int data) {
  this.data = data;
 }
 public Node getLeft() {
  return left;
 }
 public void setLeft(Node left) {
  this.left = left;
 }
 public Node getRight() {
  return right;
 }
 public void setRight(Node right) {
  this.right = right;
 }
} 

PostorderBinaryTreeTraversal.java

package tree;

import java.util.LinkedList;
import java.util.Queue;

public class PostorderBinaryTreeTraversal {
 private Node rootNode;

 public static void main(String[] args) {
  new BinaryTreeTraversal();
 }

 public PostorderBinaryTreeTraversal(){

  addNode(rootNode, 45); 
  addNode(rootNode, 25); 
  addNode(rootNode, 75); 
  addNode(rootNode, 15); 
  addNode(rootNode, 35); 
    
  System.out.println("\nPost Order Traversal :");
  printTreePostOrder(rootNode);
 }
  
 //Postorder Printing.
 private void printTreePostOrder(Node rootNode){
  if(rootNode==null)
   return;
  printTreePostOrder(rootNode.getLeft());
  printTreePostOrder(rootNode.getRight());
  System.out.print(rootNode.getData() + " ");
 }
   
 private void addNode(Node rootNode, int data){
  if(rootNode==null){
   Node temp1 = new Node(data);
   this.rootNode=temp1;
  }else{
   addNodeInProperPlace(rootNode, data);
  }
 }
  
 private void addNode(Node rootNode, int data){
  if(rootNode==null){
   Node temp1 = new Node(data);
   this.rootNode=temp1;
  }else{
   addNodeInProperPlace(rootNode, data);
  }
 }
 
 private void addNodeInProperPlace(Node rootNode, int data){ 
  if(data>rootNode.getData()){
   if(rootNode.getRight()!=null){
    addNode(rootNode.getRight(), data);
   }else{
    Node temp1 = new Node(data);
    rootNode.setRight(temp1);
   }
  }else if(data<rootNode.getData()){
   if(rootNode.getLeft()!=null){
    addNode(rootNode.getLeft(), data);
   }else{
    Node temp1 = new Node(data);
    rootNode.setLeft(temp1);
   }
  }
 }
}

You may also like to see


Preorder Traversal of Binary Tree in Java 

Zig Zag Traversal of Binary Tree. 

Boundary Traversal of binary tree. 

Print Nodes in Top View of Binary Tree

Print Nodes in Bottom View of Binary Tree.

Print a Binary Tree in Vertical Order

Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

Post a Comment