Same Tree
An easy Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 3
- Core for
- 4 roles
The problem
Two binary trees p and q are given. Decide whether they are identical, meaning they have exactly the same shape and every matching position holds the same value.
Example 1
- Input
- p=[7,3,9], q=[7,3,9]
- Output
- true
- Why
- Same shape, same values at every position.
Example 2
- Input
- p=[4,6], q=[4,null,6]
- Output
- false
- Why
- One 6 hangs left, the other right: the structures differ.
Example 3
- Input
- p=[5,8,2], q=[5,2,8]
- Output
- false
- Why
- Same shape, but 8 and 2 are swapped between the children.
Constraints
- The number of nodes in both trees is in the range [0, 100].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-29Two trees match if and only if their roots match in value and existence, and their respective left and right subtrees match recursively. Both shape and values must align synchronously at every step; a single structural divergence halts comparison immediately.
Approaches, worst first
Iterative queue pairing
time O(n) · space O(n)
Push roots as a pair into a queue. In each iteration, pop two nodes: if both null, continue; if one null or values differ, return false; otherwise enqueue their left and right child pairs in tandem.
Simultaneous recursive descentWrite this one
time O(n) · space O(h)
If both pointers are null, return true. If exactly one is null or node values disagree, return false. Otherwise return the logical AND of recursively checking both left subtrees and both right subtrees.
Where people lose marks · 3
- Assuming identical preorder traversals imply identical trees; traversals without explicit null markers produce false positives on structural differences like left versus right children.
- Checking `p.val === q.val` before confirming neither pointer is null triggers null property dereferences.
- Returning true when only one of `p` or `q` hits null instead of enforcing both to be null simultaneously.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .