Postorder Traversal in Binary Tree in java

What is Postorder Traversal ?

In postorder traversal , first we traverse the left subtree, then the right subtree and finally the root node.post order traversal is used to get the postfix expression of an expression given. In this article we will see how to perform postorder traversal in java.

post order traversal using recursion in java

Steps to find Postorder traversal

    Here are some of the steps to find postorder traversal :
  • Step 1: Print the left most child of left subtree of binary tree i.e 20.
  • Step 2: Now , before printing the root node, move to right sub-tree and print the left child i.e. 40.
  • Step 3: Print 50 which is right child.
  • Step 4: Now, print it’s root node 30.
  • Step 5: At last print the root of the tree i.e. 10.

The printing sequence will be  20,40,50,30,10.

post order traversal in binary tree

Algorithm to find Postorder traversal of binary tree using recursion

Algorihtm postorder(Tree):

  1. Recursively traverse left sub-tree.
  2. Recursively traverse right sub-tree.
  3. Visit root node.
Run
//Postorder Traversal 
/*Node class with left and right child and current node and key value*/
class Node
{
    Node left ,right;
    int value;
    Node(int value)
    {
        this.value=value;
        left=null;
        right=null;
    }
}
class Postorder
{
    Node root; //root node of the binary tree
    Postorder()
    {
        root=null;
    }  
    /*postorder traversal of binary tree */
    public  void postorder(Node ptr)
    {
        if(ptr==null)
        return ;
        /*first traverse left child*/   
        postorder(ptr.left);
        /*then traverse the right child*/
        postorder(ptr.right);
        /*now print the value of node*/
       System.out.print(ptr.value+" ");
    }
}
public class Main{
    public static void main(String[] args)
    {
        Postorder t=new Postorder();
        t.root=new Node(10);
        t.root.left=new Node(20);
        t.root.right=new Node(30);
        t.root.right.left=new Node(40);
        t.root.right.right=new Node(50);
        t.postorder(t.root);
    }
}

Output :

20 40 50 30 10 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java