The Same Tree interview question asks you to determine whether two binary trees are structurally identical and have the same node values at every corresponding position. Both trees must have the same shape and the same values for the result to be true. This is a fundamental recursive tree comparison problem.
This problem is asked at Apple, Microsoft, Meta, Amazon, LinkedIn, Oracle, Google, Bloomberg, and Adobe as a gateway tree problem. It establishes the template for recursive tree comparison — a pattern reused in problems like Symmetric Tree, Subtree of Another Tree, and comparing BSTs. It tests understanding of simultaneous recursive traversal of two trees.
The pattern is simultaneous recursive DFS. Base cases: if both nodes are null → true. If only one is null → false. If values differ → false. Recursive case: return isSameTree(p.left, q.left) and isSameTree(p.right, q.right). Iterative BFS: use a queue of node pairs; for each pair, apply the same checks and enqueue the left and right child pairs.
Tree p: Tree q:
1 1
/ \ / \
2 3 2 3
Simultaneous DFS:
All comparisons true → return true.
Different trees:
1 1
/ \
2 2
false.Return false.
true.For the Same Tree coding problem, the DFS binary tree interview pattern is the recursive template. The three-line recursive solution is elegant and worth memorizing: check nulls, check values, recurse left and right. Interviewers at Apple and Adobe use this as a warm-up before asking Symmetric Tree or Subtree of Another Tree. Practice both the recursive and iterative (BFS queue of pairs) implementations — knowing the iterative version shows depth of understanding.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Depth of Binary Tree | Easy | Solve | |
| Symmetric Tree | Easy | Solve | |
| Invert Binary Tree | Easy | Solve | |
| Cousins in Binary Tree | Easy | Solve | |
| Merge Two Binary Trees | Easy | Solve |