Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Subtree of Another Tree

Problem in Plain English

Given two binary trees called root and subRoot, decide whether root contains a subtree with exactly the same structure and node values as subRoot. A subtree is a node together with all of its descendants, and the whole tree counts as a subtree of itself.

Problem Statement

Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values as subRoot, and false otherwise.

A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree itself is also considered a subtree of tree, so identical trees are a match.

Constraints

  • The tree root contains between 1 and 2000 nodes.
  • The tree subRoot contains between 1 and 1000 nodes.
  • Node values lie between −104-10^4 and 10410^4, inclusive.
  • Inputs are nonempty ordinary binary trees with no cycles or shared children inside either tree. Array examples use level order with null for a missing child.

Examples

Example 1

Input
root = [3,4,5,1,2], subRoot = [4,1,2]
Output
true

The node with value 4 in root has children 1 and 2, exactly matching subRoot.

Example 2

Input
root = [4], subRoot = [4]
Output
true

The entire single-node tree is a valid subtree. Its value and both absent child slots match.

Example 3

Input
root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
Output
false

The candidate node 4 looks right, but its child 2 has an extra child 0, so the structures differ.

Example 4

Input
root = [1,1,null,2], subRoot = [1,2]
Output
true

The first 1 fails because its left child is 1, not 2. The second 1 has exactly the left child 2, so searching must continue after a failed candidate.

Intuition

Separate finding a candidate root from proving an exact match. In Example 1, 3 cannot match target root 4, but its left child 4 can: every descendant and missing child agrees. A candidate with an extra descendant fails because a subtree includes all descendants; you cannot prune it to fit the target.

Approaches

Check Every Node

Solution Details

Reveal Check Every Node: intuition, complexity, and code

Hints

Hint 1
First distinguish an exact subtree comparison from a search for where that comparison should start.
Hint 2
When a candidate fails, search its children; a repeated root value may match farther down.
Hint 3
Preorder tokens need explicit missing-child markers. Match whole tokens to avoid multi-digit value collisions.
Hint 4
Use a prefix table for KMP so failed partial matches reuse compatible suffixes instead of starting from scratch.

Edge Cases

  • A one-node root equals the one-node target: the whole tree qualifies, as in Example 2.
  • The candidate has an extra descendant, as in Example 3: matching visible values is insufficient.
  • Repeated root values require continuing after failure, as in Example 4.
  • A target larger than root cannot match; both methods return false without special size logic.
  • A 2000-node chain should be processed with explicit stacks; ordinary recursive Python helpers can exceed the default recursion limit.
  • Value 2 must not match within 12, and negative values must remain distinct whole tokens.

Common Mistakes and Interview Tips

  • Checking only the main root and returning false when its value differs from the target root.
  • Returning true after a root value match, or using OR instead of AND for corresponding child equality.
  • Allowing a candidate to have extra children because the target has no node in that position.
  • Dropping null markers from serialization loses structure; [1,2] and [1,null,2] then have the same value visits.
  • Claiming built-in contains/includes guarantees a linear scan; explicit KMP establishes the bound shown here.
  • On a KMP mismatch, setting j to zero skips reusable suffixes; use lps[j - 1] until the next token is compatible.
  • Using recursive serialization on the maximum chain without accounting for language stack limits.

Key Takeaway

A structural search can be reduced to sequence matching when the encoding is unambiguous and each candidate occupies a contiguous block. Keep missing-child markers, compare whole tokens, and use a proven linear matcher to remove repeated work.