The goal is to determine if two binary trees are identical. Two trees are considered the same if they have the same structure and their corresponding nodes have the same values.
The recursive approach is straightforward for this problem. We will traverse both trees simultaneously, comparing their nodes at each step.
- Base Cases:
- Both Nodes are
null: If both nodes arenull, they are structurally identical up to this point, so we returntrue. - One Node is
nulland the Other is Not: If only one of the nodes isnull, the trees are not structurally identical, so we returnfalse. - Node Values Differ: If the values of the nodes differ, the trees are not identical, so we return
false.
- Both Nodes are
- Recursive Case:
- If the current nodes are the same, recursively check their left and right children to ensure the entire subtrees are identical.
- Time complexity: O(n), where n is the number of nodes in the trees.
- Space complexity: O(h), where h is the height of the trees.
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (!p && !q) return true;
if (!p || !q) return false;
return (p->val == q->val)
&& isSameTree(p->left, q->left)
&& isSameTree(p->right, q->right);
}
};