Postorder Traversal in Binary Tree in java
Postorder Traversal in Binary Tree
Postorder Traversal in Binary Tree in Java is a Depth First Traversal (DFS) technique where nodes are visited in the following order:
Left → Right → RootIt is also called LRN traversal. In this traversal, the root node is processed after both left and right subtrees, making it especially useful for problems like tree deletion, expression tree evaluation, and postfix expression generation.
What is Postorder Traversal?
In postorder traversal, we:
- Traverse the left subtree
- Traverse the right subtree
- Visit the root node
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.
Example of Postorder Traversal:
Methods for Postorder Traversal in Binary Tree in Java
Postorder traversal in Binary tree can be performed using 2 methods using Java:
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Methods for Postorder Traversal in Binary Tree
Method 1: Recursive Postorder Traversal
Algorithm for Postorder Traversal
postorder(node)
1. If node is null
return
2. Call postorder(node.left)
3. Call postorder(node.right)
4. Print node.data)
Java Code:
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
public class PostorderTraversal {
Node root;
void postorder(Node node) {
if (node == null)
return;
postorder(node.left);
postorder(node.right);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
PostorderTraversal tree = new PostorderTraversal();
tree.root = new Node(10);
tree.root.left = new Node(20);
tree.root.right = new Node(30);
tree.root.left.left = new Node(40);
tree.root.left.right = new Node(50);
System.out.print("Postorder Traversal: ");
tree.postorder(tree.root);
}
}
Output:
Postorder Traversal: 40 50 20 30 10
Method for Postorder Traversal in Binary Tree
Method 2: Iterative Postorder Traversal (Using Two Stack)
Algorithm for Iterative Postorder Traversal
1. Create two stacks: stack1 and stack2
2. Push root to stack1
3. While stack1 is not empty:
a. Pop node from stack1 and push to stack2
b. Push left child to stack1
c. Push right child to stack1
4. Print elements from stack2
Java Code:
import java.util.Stack;
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
public class PostorderIterative {
Node root;
void postorder() {
if (root == null)
return;
Stack stack1 = new Stack<>();
Stack stack2 = new Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
Node current = stack1.pop();
stack2.push(current);
if (current.left != null)
stack1.push(current.left);
if (current.right != null)
stack1.push(current.right);
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().data + " ");
}
}
public static void main(String[] args) {
PostorderIterative tree = new PostorderIterative();
tree.root = new Node(10);
tree.root.left = new Node(20);
tree.root.right = new Node(30);
tree.root.left.left = new Node(40);
tree.root.left.right = new Node(50);
System.out.print("Postorder Traversal: ");
tree.postorder();
}
}
Output:
Postorder Traversal: 40 50 20 30 10
Space Complexity: O(h)
where, h = height of the binary tree
Skewed Tree → O(n)
To wrapping up with
Postorder Traversal in Binary Tree in Java:
If root is null → traversal returns nothing
Check null before accessing child nodes
Ensure stacks are not empty before pop
Common Problem with Postorder traversal:
Forgetting correct order (Left → Right → Root)
Mixing preorder logic in iterative solution
Incorrect stack usage
Missing base condition
Frequently Asked Questions
Answer:
It is a traversal method where nodes are visited in Left → Right → Root order.
Answer:
Time complexity is O(n) because all nodes are visited once.
Answer:
It is used in tree deletion and postfix expression evaluation.
Answer:
Yes, using one or two stacks.
Answer:
Space complexity is O(h), where h is the height of the tree.In
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

Login/Signup to comment