↓Skip to main content
  1. LeetCode/

LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal

·1 min· ·
LeetCode Medium Array Hash-Table Divide-and-Conquer 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: medium First Attempt: 2022-11-10

  • Submissions: Accepted ×2 → Runtime Error → Accepted

Approach
#

The same idea as LC 105, but the root is last in the post-order (Tree). Taking values from the end gives root, right, left, so build the right subtree first to keep the pointer in sync.

My Attempts
#

Right after LC 105 this came quickly; only one version sliced the wrong range (ValueError).

Solution
#

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        pos = {v: i for i, v in enumerate(inorder)}
        it = reversed(postorder)
        def build(lo, hi):
            if lo > hi:
                return None
            val = next(it)
            root = TreeNode(val)
            root.right = build(pos[val] + 1, hi)         # 先建右子樹
            root.left = build(lo, pos[val] - 1)
            return root
        return build(0, len(inorder) - 1)

Complexity
#

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

Mistakes to Avoid
#

  • From the end of the post-order the order is root, right, left; build the right subtree first.

Interview Explanation
#

The last post-order value is the root, splitting the in-order. Taking roots from the end of the post-order, I build the right subtree first, then the left, with in-order positions from a dictionary. Time O(n).

Related

LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
·2 mins
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 105 note: the first pre-order value is the root, and its position in the in-order tells the left subtree’s size; look positions up in a dictionary for O(n)
LeetCode 347: Top K Frequent Elements
·1 min
LeetCode Medium Array Hash-Table Divide-and-Conquer Sorting
LeetCode 347 note: count first, then take the k most frequent with a size-k heap or a bucket sort indexed by count
LeetCode 215: Kth Largest Element in an Array
·2 mins
LeetCode Medium Array Divide-and-Conquer Sorting Heap-Priority-Queue
LeetCode 215 note: keep a min-heap of size k whose top is the k-th largest, or use quickselect for O(n) on average
LeetCode 973: K Closest Points to Origin
·1 min
LeetCode Medium Array Math Divide-and-Conquer Geometry
LeetCode 973 note: as in Top-K, keep a size-k max-heap (negated distances) holding the k closest points
LeetCode 128: Longest Consecutive Sequence
·2 mins
LeetCode Medium Array Hash-Table Union-Find
LeetCode 128 note: put everything in a set and count upward only from values whose x - 1 is absent; each value is seen at most twice, O(n)
LeetCode 73: Set Matrix Zeroes
·2 mins
LeetCode Medium Array Hash-Table Matrix
LeetCode 73 note: record which rows and columns contain a 0 first, then update afterward; the follow-up uses the first row and column as markers