Description
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. Return true or false.
Examples
Input:
p = [1,2,3], q = [1,2,3]Output:
trueExplanation:
Comparing node by node: both roots are 1, both left children are 2, both right children are 3. Structure and values match at every position.
Input:
p = [1,2], q = [1,null,2]Output:
falseExplanation:
Tree p has node 2 as the left child of root 1, while tree q has node 2 as the right child. Despite having the same values, their structures differ.
Input:
p = [], q = []Output:
trueExplanation:
Both trees have null roots with no nodes. Two empty trees are structurally identical by definition, so they are considered the same.
Constraints
- •
The number of nodes in both trees is in the range [0, 100] - •
-10⁴ ≤ Node.val ≤ 10⁴