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 INPUT Binary Tree Structure: 1 2 3 null Input Array: [1, null, 2, 3] ALGORITHM STEPS 1 Visit Left Subtree Node 1 has no left child 2 Visit Right Subtree Go to node 2 3 Process Node 2's Left Visit node 3, add to result 4 Backtrack and Process Add 2, then add 1 Traversal Order: Left --> Right --> Root 3rd 2nd 1st FINAL RESULT Nodes visited in order: 3 1st 2 2nd 1 3rd Output Array: [3, 2, 1] OK - Correct! Time: O(n) Space: O(h) - height Optimal recursive solution Key Insight: Postorder traversal processes nodes in Left --> Right --> Root order. This is useful when you need to process children before parents, such as deleting a tree or evaluating expression trees. The recursive approach naturally handles this by making recursive calls before processing the current node. TutorialsPoint - Binary Tree Postorder Traversal | Optimal Recursive Solution
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