↓Skip to main content
  1. LeetCode/

LeetCode 145: Binary Tree Postorder Traversal

·2 mins· ·
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-20

  • Submissions: Runtime Error ×3 → Accepted ×3 → Runtime Error ×2 → Wrong Answer ×2 → Accepted ×2

Approach
#

Post-order is left, right, root: the root waits until both children are done (Tree). Push (node, visited):

  • First pop (visited = False): push itself back as True, then the right child, then the left (pushed last, handled first).
  • Second pop (visited = True): both children are done, so record it.

My Attempts
#

For an empty tree the first versions returned None; one put the nodes themselves in the answer instead of values; one pushed left before right, so the right side was processed first: with root 9, left 8 and right 5 the answer is [8, 5, 9]; it returned [5, 8, 9].

Solution
#

class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res, stack = [], [(root, False)]
        while stack:
            node, visited = stack.pop()
            if not node:
                continue
            if visited:
                res.append(node.val)
            else:
                stack.append((node, True))
                stack.append((node.right, False))
                stack.append((node.left, False))
        return res

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • Push right then left so the left is processed first.
  • Return values, not nodes.

Interview Explanation
#

Post-order records the root after both children, so the stack holds a node plus whether it was expanded. The first pop pushes it back with its right and left children; the second pop records it. Time O(n).

Related

LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first
LeetCode 94: Binary Tree Inorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 94 note: iterative in-order traversal, pushing nodes while going left, recording on pop, then turning to the right subtree
LeetCode 110: Balanced Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 110 note: every node’s subtree heights must differ by at most 1; return heights in post-order and propagate -1 once unbalanced
LeetCode 111: Minimum Depth of Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 111 note: minimum depth must end at a leaf; with only one child, the empty side (depth 0) cannot count as the minimum
LeetCode 112: Path Sum
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 112 note: subtract each value from targetSum on the way down; it works if the remainder matches at a leaf
LeetCode 100: Same Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 100 note: two empty trees are equal, one empty and one not differ, and otherwise compare values and recurse on both sides