36 / 75
Recursively check if two trees have identical structure and node values.
Example:
Input:
p=[1,2,3], q=[1,2,3]Output:
trueCommon Mistakes:
- Not handling null cases properly (both null vs one null)
- Checking structure without checking values
- Using && instead of proper null checks (causes NPE)
- Overcomplicating with iterative approach
Notes:
Simple recursive comparison. Time O(min(n,m)), Space O(min(h1,h2)) for recursion.
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
Recursively check if two trees have identical structure and node values.
Example:
Input:
p=[1,2,3], q=[1,2,3]Output:
trueCommon Mistakes:
- Not handling null cases properly (both null vs one null)
- Checking structure without checking values
- Using && instead of proper null checks (causes NPE)
- Overcomplicating with iterative approach
Notes:
Simple recursive comparison. Time O(min(n,m)), Space O(min(h1,h2)) for recursion.
36/75
Same Tree