Binary Tree Postorder Traversal

Easy

Topics
TreeDFSStack

Given the root of a binary tree, return its postorder traversal (left, right, node) as a list of values.

Example 1

133122
visited first
The small number above each node is its position in the output: left subtree, right subtree, node, giving [3, 2, 1]. Children are finished before their parent, which is what any bottom-up computation needs. The iterative version swaps the call stack for an explicit one.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,null,2,3]
Output: [3,2,1]

Example 2

4123521534
visited first
Input:  root = [1,2,3,4,5]
Output: [4,5,2,3,1]

Constraints

  • 0 <= number of nodes <= 100
  • -100 <= Node.val <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Postorder visits the node after both subtrees: left, right, then node. It is the order you would use to safely delete a tree.

Approach

  1. Recurse left, then recurse right.
  2. Append the current node’s value last.

Dry Run

        1
      /   \
     2     3
    / \
   4   5
The descent is identical for all three traversals -- only the position of
res.append moves. Here it is last:
  at 1: descend left
    at 2: descend left
      at 4: descend left, descend right, both null
      at 4: VISIT
    at 2: descend right
      at 5: descend left, descend right, both null
      at 5: VISIT
    at 2: VISIT
  at 1: descend right
    at 3: descend left, descend right, both null
    at 3: VISIT
  at 1: VISIT
answer 4 5 2 3 1
The root is visited LAST, and node 2 is visited only after both 4 and 5. That is
the defining property: when a node is reached, every one of its descendants has
already been handled.
On this one tree all three orders are worth holding side by side, because the
call sequence above never changes:
  preorder    1 2 4 5 3      append before descending
  inorder     4 2 5 1 3      append between the two descents
  postorder   4 5 2 3 1      append after both descents
Postorder is the one to reach for when a node's answer depends on its children's
answers -- freeing a tree, summing subtrees, measuring heights. The dependency
runs upward, so the visit has to come last.

Complexity

  • Time — O(n): each node visited once.
  • Space — O(h): stack up to height h.

Common Pitfalls

  • The visit comes after both recursive calls. Moving it above dfs(node.right) gives inorder, not postorder -- 4 2 5 1 3 where postorder is 4 5 2 3 1.
  • Postorder is the traversal to reach for when a node's answer depends on its children's answers -- deleting a tree, computing subtree sums, sizing subtrees. That dependency is the reason the visit is last.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def postorderTraversal(self, root):
        res = []
        def dfs(node):
            if not node:
                return
            dfs(node.left)
            dfs(node.right)
            res.append(node.val)
        dfs(root)
        return res
SensAI
Beta
Tap the mic to stop voice mode