Java traverse solution


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

  • 1
    Y

    inOrder should be replaced with isSub?


  • 0

    @yl Thanks. :) Corrected.


Log in to reply
 

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