#106

Construct Binary Tree from Inorder and Postorder Traversal

Medium Divide by root position

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Rebuild a binary tree (with unique values) from its inorder and postorder traversals.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: rebuild from inorder and postorder.

Why this works: postorder's last value is the root. Read postorder backwards: root, then the right subtree, then the left.

Example: in [9, 3, 15, 20, 7], post [9, 15, 7, 20, 3]

root 3 → build right from 20 (left 15, right 7) → then left 9

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Building left before right while reading postorder backwards.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Binary Trees
🎬 Episodes 53–60: video coming soon