Simple Java AC 0ms Solution


  • 0
    S
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p==null || q==null)
                return p==q;
            return p.val==q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
        }
    }

  • 0
    L

    also 0ms but not as clean as yours:

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p!=null && q != null){
            if(p.val == q.val){
                return (isSameTree(p.left, q.left) && isSameTree(p.right, q.right));
            }else{
                return false;
            }
        }else{
            return p== null && q == null;
        }
    }

Log in to reply
 

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