def is_same_tree(p,q):
if not p and not q: return True
if not p or not q: return False
return p.val==q.val and is_same_tree(p.left,q.left) and is_same_tree(p.right,q.right)
public boolean isSameTree(TreeNode p,TreeNode q){
if(p==null&&q==null) return true;
if(p==null||q==null) return false;
return p.val==q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
Python truthiness vs Java null checks. 'and' vs '&&'.
def is_same_tree(p,q):
if not p and not q: return True
if not p or not q: return False
return p.val==q.val and is_same_tree(p.left,q.left) and is_same_tree(p.right,q.right)
public boolean isSameTree(TreeNode p,TreeNode q){
if(p==null&&q==null) return true;
if(p==null||q==null) return false;
return p.val==q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
1. Both null: true 2. One null: false 3. Compare and recurse