100. Same Tree
Asked at Google, Meta, Amazon, Microsoft and 2 more
Difficulty
Easy
Time
O(n)
Space
O(h)
Pattern
Depth-First Search
Companies
6
Problem
You are given the roots of two binary trees, p and q. Decide whether the two trees are the same.
Two trees count as the same only when they match in two ways at once: the shape is identical (every node sits in the same position, including missing children) and every matching pair of nodes holds the same value.
Return true if the trees are identical, otherwise false. Both trees may be empty — two empty trees are the same.
Same shape AND same values
Constraints
- ●
The number of nodes in both trees is in the range [0, 100]. - ●
-10^4 <= Node.val <= 10^4
Examples
p = [1,2,3], q = [1,2,3]truep = [1,2], q = [1,null,2]falsep = [1,2,1], q = [1,1,2]falseAsked At
6 companies| Company | Difficulty | |
|---|---|---|
| Easy | View all Google questions → | |
| Meta | Easy | View all Meta questions → |
| Amazon | Easy | View all Amazon questions → |
| Microsoft | Easy | View all Microsoft questions → |
| Apple | Easy | View all Apple questions → |
| Uber | Easy | View all Uber questions → |
How to Think About It
Recursive: both null → same. One null, one not → different. Values differ → different. Otherwise, recurse on left children and right children.
The three checks in order: (1) are both nodes null? (2) is one null and the other not? (3) do values differ? If none of these, recurse.
BFS alternative: use two queues, push roots of both trees. Process both queues simultaneously. Compare values and children positions.
Edge cases: both null (true), one null (false), different structures (false), same structure different values (false).
Optimal Approach
Recursive DFS:
Step 1: If both p and q are null → return true.
Step 2: If one is null and the other isn't → return false.
Step 3: If values differ → return false.
Step 4: Recurse: sameTree(p.left, q.left) AND sameTree(p.right, q.right).
The order matters: check null first (base case), then values, then recurse.
Time: O(n) — visit every node once. Space: O(h) — recursion stack.
Compare node pairs top-down
What Trips People Up in Real Interviews
Only checking values, not structure. Two trees can have the same values but different structures. Check both.
Forgetting to check if both nodes are null. Two null nodes are considered the same tree.
Not handling the case where one node is null and the other isn't. This means the trees are different.
Confusing "same tree" with "identical structure." Same tree means both structure AND values must match.
Not checking values after confirming both nodes exist. After ruling out null mismatches, you must compare p.val != q.val before recursing — skipping this gives wrong results for same-structure different-value trees.
Solution Code
O(n) timedef isSameTree(p, q):
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return isSameTree(p.left, q.left) and isSameTree(p.right, q.right)Frequently Asked Questions
What is the Same Tree problem?
Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two trees are the same if they are structurally identical and have the same node values.
How do you solve Same Tree?
The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Same Tree?
Same Tree is asked at Google, Meta, Amazon, Microsoft, Apple, Uber. It is a easy difficulty problem.
What are common mistakes on Same Tree?
- Only checking values, not structure. Two trees can have the same values but different structures. Check both.
- Forgetting to check if both nodes are `null`. Two `null` nodes are considered the same tree.
- Not handling the case where one node is `null` and the other isn't. This means the trees are different.
- Confusing "same tree" with "identical structure." Same tree means both structure AND values must match.
- Not checking values after confirming both nodes exist. After ruling out `null` mismatches, you must compare p.val != q.val before recursing — skipping this gives wrong results for same-structure different-value trees.