3 Lines Recursive Solution

  • 0
        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. If both trees are null they are the same
    2. if one of the trees is null and the other is not null they are not
      the same
    3. if they are both not null they are the same tree only if the root
      has the same val and both left and right subtree are the same

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.