Determine If One Binary Tree Is a Substructure of Another
Company: Bytedance
Role: Backend Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates practical tree traversal skills and the ability to implement structural matching between two binary trees. It tests recursive tree comparison, a core algorithmic competency commonly assessed in coding interviews for software and backend engineering roles.
Constraints
- 0 <= number of nodes in A <= 10^4
- 0 <= number of nodes in B <= 10^4
- -10^4 <= Node.val <= 10^4
- Trees are given as level-order arrays where null marks a missing child
- An empty B is never a substructure; if A is empty the answer is always false
Examples
Input: ([3, 4, 5, 1, 2], [4, 1])
Expected Output: True
Explanation: Starting at A's node 4, B's root 4 matches and B's left child 1 matches A's left child 1. B has no right child, so A's right side is ignored. Valid substructure.
Input: ([3, 4, 5, 1, 2], [4, 1, 3])
Expected Output: False
Explanation: At A's node 4 the left child matches (1 vs 1) but B requires right child 3 while A's node 4 has right child 2. No other A node matches B's root 4.
Input: ([], [1])
Expected Output: False
Explanation: A is empty, so the answer is always false.
Input: ([1, 2, 3], [])
Expected Output: False
Explanation: B is empty (null root), which is never a substructure of any tree.
Input: ([1], [1])
Expected Output: True
Explanation: Single-node B matches the single-node A by value.
Input: ([10, 12, 6, 8, 3, None, None], [12, 8])
Expected Output: True
Explanation: Starting at A's node 12, B's root 12 matches and B's left child 8 matches A's left child 8. Sparse positions in A (explicit None) are handled by the level-order builder.
Input: ([8, 8, 7, 9, 2, None, None, None, None, 4, 7], [8, 9, 2])
Expected Output: True
Explanation: The second 8 in A (left child of root) has children 9 and 2, matching B exactly. Demonstrates that the matching root may be deeper in A and that duplicate values force a full subtree check, not just a root-value match.
Input: ([1, 2, 2, 3, 3, 3, 3], [2, 3, 3])
Expected Output: True
Explanation: Either node 2 in A roots a subtree whose children are 3 and 3, matching B. Duplicate values throughout.
Input: ([-1, -2, -3], [-2])
Expected Output: True
Explanation: Negative values; B's single node -2 matches A's left child.
Input: ([4, 5, 6], [4, 5, 7])
Expected Output: False
Explanation: Near miss: B's left child 5 matches but B's right child 7 differs from A's right child 6, and no other A node has value 4.
Hints
- Decompose the problem into two pieces: (1) for a FIXED node in A, does B match as a top-down prefix starting there? and (2) try every node of A as that fixed starting point.
- The prefix-match helper bottoms out the moment B runs out of nodes (return true) — B does not need to reach A's leaves. It fails only when A is missing a node that B still requires, or the values differ.
- Handle the empty cases up front: an empty B returns false, and an empty A returns false. Don't let an empty B accidentally match (a null B subtree DOES match during the recursive prefix check, but the overall empty-B input must short-circuit to false).