Skip to main content

10. Same Tree

easyAsked at Vercel

Short answer

  • Best approach: Recursive structural comparison. Both null → equal. One null → unequal. Otherwise values match AND both subtrees recursively match.
  • Time O(n), space O(h) stack
  • How often Vercel asks it: sometimes asked · Verified

Given two binary trees, decide if they are structurally identical with the same values at every position. Vercel uses this as a recursion warm-up before deeper tree questions about their route trees.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)— Vercel new-grad screen recursion warm-up.
  • LeetCode Discuss (2026-Q1)— Listed in Vercel interview prep notes.

Problem

Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

Constraints

  • The number of nodes in both trees is in the range [0, 100].
  • -10^4 <= Node.val <= 10^4

Examples

Example 1

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

Example 2

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

Approaches

1. Serialize and compare strings

Serialize each tree to a canonical string with null markers and compare.

Time
O(n)
Space
O(n)
function isSameTree(p, q) {
  const ser = (n) => n === null ? '#' : `${n.val},${ser(n.left)},${ser(n.right)}`;
  return ser(p) === ser(q);
}

Tradeoff: Works but allocates a full string per tree. The recursive comparison is more direct and equally fast.

2. Recursive structural comparison (optimal)

Both null → equal. One null → unequal. Otherwise values match AND both subtrees recursively match.

Time
O(n)
Space
O(h) stack
function isSameTree(p, q) {
  if (!p && !q) return true;
  if (!p || !q) return false;
  if (p.val !== q.val) return false;
  return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}

Tradeoff: Clean structural recursion. Short-circuits on the first mismatch via &&.

Vercel-specific tips

Vercel grades for the explicit null handling in the right order: both null, then one null, then value check. Bonus signal: pointing out that && short-circuits, so you don't recurse into the right subtree if the left doesn't match.

Common mistakes

  • Checking `p.val !== q.val` before the null guards — crashes on `p` being null.
  • Returning true when one is null and the other isn't — confuses 'no node' with 'matching node'.
  • Using == instead of === on values — works for numbers but breaks for null vs undefined.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Symmetric tree (LC 101) — same pattern, mirror comparison.
  • Subtree of another tree (LC 572) — same pattern, applied at every node.
  • Iterative version using a queue of (p, q) pairs.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why three null checks?

Both-null is the success base case. One-null is the failure case. After those, both are non-null and you can safely access .val.

Why does the short-circuit matter?

On large trees, the first mismatch in the left subtree should prevent traversing the right. JavaScript's && evaluates left-to-right and skips the right operand if the left is false — exactly the behavior we want.

Originally published on InterviewChamp.AI

Companies that also ask Same Tree