Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Same Tree

Problem in Plain English

Given the roots of two binary trees p and q, return true when both trees share the same structure and the same values at every position. Two empty trees count as identical.

Problem Statement

Given the roots p and q of two binary trees, check whether they are the same tree.

Two trees are the same when they are structurally identical and every pair of nodes in matching positions holds equal values. Two empty trees are the same.

Constraints

  • Each tree contains between 0 and 100 nodes.
  • Node values lie between −104-10^4 and 10410^4, inclusive.
  • Inputs are ordinary binary trees: no cycles or shared children. Array examples use level order with null for a missing child.

Examples

Example 1

Input
p = [1,2,3], q = [1,2,3]
Output
true

Both trees place 1 at the root with 2 left and 3 right.

Example 2

Input
p = [], q = []
Output
true

Both roots are absent. There is no position at which shape or value differs, so two empty trees match.

Example 3

Input
p = [1,2], q = [1,null,2]
Output
false

2 is a left child in p but a right child in q, so the structures differ.

Example 4

Input
p = [1,2,1], q = [1,1,2]
Output
false

The shapes match but the children hold swapped values.

Intuition

Equal roots are only the first check: every left and right position must also agree. In Example 1, pair 1 with 1, then 2 with 2 and 3 with 3; the empty child slots must match too. Equal values reached in different positions do not make identical trees.

Approaches

Pair Queue Check

Solution Details

Reveal Pair Queue Check: intuition, complexity, and code

Hints

Hint 1
Visit matching positions together: the left child of one with the left child of the other, and likewise for the right.
Hint 2
Two empty slots in the same position are fine, but one empty slot against a real node ends the check.
Hint 3
Equal values still need equal subtrees, so confirm the current pair and recurse into both child pairs.

Edge Cases

  • Two empty trees match without any comparison.
  • One empty tree against a non-empty tree mismatches at the very first pair.
  • Equal values in mirrored positions still mismatch because the structure check pairs left with left.

Common Mistakes and Interview Tips

  • Reading .val before ruling out empty nodes, which crashes on the first missing child.
  • Comparing only values while ignoring shape, which wrongly accepts mirrored placements.
  • Returning true after the roots match without checking the subtrees below.
  • Comparing preorder value lists without null markers loses shape: [1,2] and [1,null,2] both visit values 1,2.
  • Using JavaScript Array.shift() repeatedly can add quadratic movement of queue entries; advance a head index.
  • Using OR to combine child matches accepts a tree when only one side agrees.

Key Takeaway

Dual-tree questions stay aligned by carrying both nodes together, whether as a recursive pair of arguments or as pairs in a queue.