easy
binary-tree
depth-first-search
recursion
Return true when binary trees p and q have the same structure and equal values.
Constraints
- 0 <= total nodes <= 2000
- -10000 <= node value <= 10000
Follow-up
How would an iterative queue-based comparison work?
Execution note
Java/Go/Rust don't support this problem's ListNode/TreeNode structure yet — the starter is reference-only. Try Python or JavaScript to run and submit.
Examples
Example 1
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3
Input: p = [1,2,1], q = [1,1,2]
Output: false
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.