My Solution


  • 0
    H

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

    private boolean isSame(TreeNode p, TreeNode q){
        if(p == null && q == null)
            return true;
        if(p == null || q == null)
            return false;
        if(p.val != q.val)
            return false;
        return isSame(p.left, q.left) && isSame(p.right, q.right);
    }

Log in to reply
 

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