coding•DSA & Problem Solving•Trees & Binary Search Trees
Same Tree
Problem Statement
Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
### Constraints
- The number of nodes in both trees is in the range `[0, 100]`.
- `-10^4 <= Node.val <= 10^4`
### Input Format
First line contains space-separated integers for tree `p` in level-order (-1 for null).
Second line contains space-separated integers for tree `q` in level-order (-1 for null).
### Output Format
Print `true` if the trees are identical, otherwise `false`.
Sample Cases
Example 1Sample Case
Input: 1 2 3
1 2 3
Output: true
Example 2Sample Case
Input: 1 2
1 -1 2
Output: false
Example 3Sample Case
Input: -1 -2 -3
-1 -2 -3
Output: true
Constraints
Runtime Limit: 2000 ms per test batch
Memory Limit: 256 MB
Standard I/O stream reading
Official submissions are evaluated against hidden test cases on the server to verify time & memory complexity.