Trees · Coding problem
Same Tree
A free coding problem from the Binary Tree DFS Traversal Orders skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
Two binary trees are the same when they have the same shape and every pair of matching nodes holds the same value. Given two trees, return whether they are the same.
Input format
Two trees, each in the usual heap-array format, one after the other.
Example
Input:
3
4 7 9
3
4 9 7
Output: false
Same shape, but the 7 and the 9 have swapped sides.
Constraints
0 <= number of nodes in each tree <= 100-10^4 <= node value <= 10^4
How it's graded
- Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Binary Tree DFS Traversal Orders
Solve it Same Tree free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime