Skip to main content

11. Same Tree

easyAsked at Reddit

Short answer

  • Best approach: Synchronized recursion. Recurse both trees together. Both null = true. Either null but not both = false. Values differ = false. Otherwise recurse both children.
  • Time O(n), space O(h)
  • How often Reddit asks it: sometimes asked · Verified

Given two binary trees, determine if they are structurally identical with the same values. Reddit uses this to test recursion fundamentals — the basis for comparing two snapshots of a comment subtree during edit-diff display.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)— Reddit phone screen for backend roles.

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

Example 3

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

Approaches

1. Serialize both and compare

Convert both trees to canonical strings (with null markers) and string-compare.

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

Tradeoff: Linear time but builds O(n) intermediate strings. Allocates more than necessary.

2. Synchronized recursion (optimal)

Recurse both trees together. Both null = true. Either null but not both = false. Values differ = false. Otherwise recurse both children.

Time
O(n)
Space
O(h)
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: Short-circuits early on first mismatch. O(h) recursion stack.

Reddit-specific tips

Reddit interviewers grade on whether you handle the three cases cleanly: (both null, one null, both non-null). Bonus signal: mention that early termination matters in production for shallow diffs (a single value change at the root saves traversing the whole tree).

Common mistakes

  • Returning true when only one side is null (must be both).
  • Comparing nodes directly (object identity) instead of .val.
  • Forgetting && — the children must BOTH match.

Follow-up questions

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

  • Symmetric tree (LC 101) — same idea, mirrored.
  • Subtree of another tree (LC 572).
  • Find duplicate subtrees (LC 652).

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 early-return on null cases before value check?

Otherwise p.val on a null node throws. Order matters.

Is BFS equivalent?

Yes — push both queues in lockstep. Same complexity, slightly more code.

Originally published on InterviewChamp.AI

Companies that also ask Same Tree