isSubTree

public boolean isSubtree(TreeNode root1, TreeNode root2) {
    if(root1 == null) return true;
    else if(root2 == null) return false;
    else if(root1.val == root2.val) {
        if(isEqual(root1.left, root2.left) && isEqual(root1.right, root2.right)) {
            return true;
        }
    }
    return isSubtree(root1, root2.left) || isSubTree(root1, root2.right);
}

public boolean isEqual(TreeNode root1, TreeNode root2) {
    if(root1 == null) return true;
    else if(root2 == null) return false;
    else return (root1.val == root2.val) && isEqual(root1.left, root2.left) && isEqual(root1.right, root2.right);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章