To determine if two binary trees are the same, we need to check if they are structurally identical and if each corresponding node has the same value. This can be efficiently done using a recursive approach that compares each node pair-wise.
- Base Cases:
- If both nodes (
p
andq
) are null, they are considered the same, so return true. - If one of the nodes is null and the other is not, they are not the same, so return false.
- If the values of
p
andq
are different, return false since this indicates they are not identical.
- If both nodes (
- *Recursive Comparison:
- Recursively check the left children of
p
andq
. - Recursively check the right children of
p
andq
. - Both left and right subtrees must match for the trees to be considered identical.
- Recursively check the left children of
- Return the Result: Use the && operator to ensure that the current nodes have the same value and that both left and right subtrees are identical.
- Time Complexity:
O(n)
, wheren
is the number of nodes in the smaller tree (if both trees have the same structure, thenn
is the total number of nodes in either tree). Each node is visited once. - Space Complexity:
O(h)
, whereh
is the height of the tree. This represents the maximum depth of the recursion stack. In the worst case (for a skewed tree),h = n
; in the best case (for a balanced tree),h = log(n)
.
This solution checks if two binary trees are the same by recursively comparing each node’s value and structure. By traversing both trees simultaneously, we ensure that they are identical in both structure and values, achieving an efficient `O(n) time complexity.