Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.buildTree(inorder, postorder). Values are unique and both traversals describe the same binary tree. Return its root.

Starter code

class Solution:
    def buildTree(self, inorder, postorder):
        pass
Test cases

sample

{
  "args": [
    [
      9,
      3,
      15,
      20,
      7
    ],
    [
      9,
      15,
      7,
      20,
      3
    ]
  ]
}

Expected: {"$type":"binary-tree","values":[3,9,20,null,null,15,7]}

Wizard outline
  1. Step 1: Initialize Solution.buildTree

    Replace the empty starter with the first real state owned by Solution.buildTree. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Right Chain case

    Complete the readable core algorithm for one representative Interview case. Consume postorder from the end and build right before left.

  4. Step 4: Harden the Sample boundary

    Repair the reviewed boundary and pass the complete submission contract. Backward postorder exposes each subtree root followed by its right then left subtree roots. Inorder positions uniquely delimit those intervals, so the recursion reconstructs the exact tree.

Footguns and prerequisites
  • Backward postorder visits root, right, left, so constructing left first consumes the wrong root.
  • trees and graphs
  • hashing and sets
Reviewed references
Practice prerequisites
  • Partition Traversal Ranges(opens in a new tab)

    Partition Traversal Ranges isolates each frame describes matching node sets, and the inorder root offset determines the exact left-subtree size in preorder. That focused state discipline is required when implementing build tree inorder postorder as a complete Interview Problem.

Recommended approach and implementation

Map inorder indices, keep a cursor at postorder end, and recursively build inclusive intervals root-right-left.

Why it works: Backward postorder exposes each subtree root followed by its right then left subtree roots. Inorder positions uniquely delimit those intervals, so the recursion reconstructs the exact tree.

class Solution:
    def buildTree(self, inorder, postorder):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        positions = {value:index for index,value in enumerate(inorder)}
        postorder_index = len(postorder) - 1
        def build(left, right):
            nonlocal postorder_index
            if left > right: return None
            value = postorder[postorder_index]
            postorder_index -= 1
            node = TreeNode(value)
            middle = positions[value]
            node.right = build(middle + 1, right)
            node.left = build(left, middle - 1)
            return node
        return build(0, len(inorder) - 1)
Similar exercises