10. Same Tree
easyAsked at FigmaGiven the roots of two binary trees, determine whether they are structurally identical and have the same values. Figma uses this to check tree-diffing fluency, the foundation of their multiplayer scene-graph reconciliation step.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Figma loops.
- Glassdoor (2026-Q1)— Figma onsite, paired with multiplayer-reconciliation discussion.
- LeetCode Discuss (2025)— Easy bucket of Figma OA.
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
p = [1,2,3], q = [1,2,3]trueExample 2
p = [1,2], q = [1,null,2]falseApproaches
1. Serialize and compare strings
Serialize both trees with a deterministic null-marker scheme; compare strings.
- 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 but allocates a giant string. Hides the structural recursion that interviewers want to see.
2. Recursive structural compare
Both null => true; one null => false; values differ => false; else recurse on left and right.
- 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: Clean, expressive, and short-circuits on the first mismatch. Best for showing recursive thinking.
Figma-specific tips
Figma loves the recursive form because their multiplayer reconciler walks two scene graphs in lockstep doing exactly this comparison. Lead with the three-case taxonomy ('both null / one null / both present') aloud, and explicitly mention that the && short-circuits on the first mismatch — that's the Figma-style observation about avoiding unnecessary tree walks under load.
Common mistakes
- Checking p.val !== q.val before the null guards — null pointer exception.
- Returning early on the left subtree without checking the right when both match the value.
- Using == instead of === for value comparison (rarely matters here but signals carelessness).
Follow-up questions
An interviewer at Figma may pivot to one of these next:
- Symmetric Tree (LC 101) — same skeleton but compares left/right mirror.
- Subtree of Another Tree (LC 572) — uses isSameTree as a primitive.
- Tree edit distance — generalize to a diff cost.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
What if both trees are empty?
They're considered the same. The first base case (!p && !q => true) handles it.
Could I use BFS instead?
Yes — walk both with the same queue order. DFS recursion is shorter and stack-safe up to ~10^4 depth which fits this problem.
Practice these live with InterviewChamp.AI
Drill Same Tree and other Figma interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →