class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
} else if (p == null || q == null) {
return false;
}
return p.val == q.val
&& isSameTree(p.left, q.left)
&& isSameTree(p.right, q.right);
}
}
Let m
be number of TreeNodes
in p
. Let n
be number of TreeNodes
in q
.
- Time Complexity: O(min(m + n))
- Space Complexity: O(min(m + n))