Introduction
Postorder Traversal means:
- traversing binary tree
- in Left → Right → Root order
Traversal Order:
Left SubtreeRight Subtree
Current Node
Example:
1
/ \
2 3
/ \
4 5
Postorder Traversal:4 5 2 3 1
Explanation:
First visit: left subtree
Then:
right subtree
Finally:current node
This problem is one of the most important applications of:
DFS TraversalConstraints
1 <= Number of Nodes <= 10^5 Approach : Recursive DFS Solution
Explanations:
Explanation:
The idea is:
- recursively traverse left subtree
- recursively traverse right subtree
- process current node
Steps:
- Traverse left subtree.
- Traverse right subtree.
- Visit current node.
Traversal Pattern:
Left → Right → Root This approach:
- uses recursion
- follows DFS traversal
Dry Run
Tree:1
/ \
2 3
/ \
4 5
Go left:
2
Go left:
4
Visit:
4
Backtrack:
2
Go right:
5
Visit:
5
Visit:
2
Go right:
3
Visit:
3
Visit:
1
Output:
4 5 2 3 1
Practice :