Binary Tree Postorder Traversal - Problem

Given the root of a binary tree, return the postorder traversal of its nodes' values.

In postorder traversal, we visit nodes in the order: left subtree → right subtree → current node. This means we process all children before processing the parent node.

Note: You may implement this using either recursive or iterative approaches.

Input & Output

Example 1 — Basic Tree
$ Input: root = [1,null,2,3]
Output: [3,2,1]
💡 Note: Postorder visits left→right→root: node 3 (leaf), then node 2, finally root 1
Example 2 — Empty Tree
$ Input: root = []
Output: []
💡 Note: Empty tree returns empty array
Example 3 — Single Node
$ Input: root = [1]
Output: [1]
💡 Note: Single node tree: only root to process

Constraints

  • The number of nodes in the tree is in the range [0, 100]
  • -100 ≤ Node.val ≤ 100

Visualization

Tap to expand
Binary Tree Postorder Traversal Overview123Root (3rd)2nd1stPostorder Process1. Visit left subtree of 1 → null2. Visit right subtree of 1 → node 23. Visit left subtree of 2 → node 34. Process leaf 3 → add to result [3]5. Process node 2 → add to result [3,2]6. Process root 1 → add to result [3,2,1]Final Result[3, 2, 1]
Understanding the Visualization
1
Input Tree
Binary tree with nodes [1,null,2,3]
2
Traversal Order
Visit left→right→root: 3, 2, then 1
3
Output Array
Collect node values: [3,2,1]
Key Takeaway
🎯 Key Insight: Postorder processes all children completely before touching the parent node
Asked in
Google 45 Amazon 38 Microsoft 32 Facebook 28
125.0K Views
Medium Frequency
~15 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen