java 10-line AC solution


  • 0
    A
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s==null && t==null) return true;
        if(s==null || t==null) return false;
        return isSame(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);        
    }
    public boolean isSame(TreeNode s, TreeNode t){
        if(s==null && t==null) return true;
        if(s==null || t==null) return false;
        return s.val==t.val && isSame(s.left, t.left) && isSame(s.right, t.right);
    }

Log in to reply
 

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