Leetcode:剑指offer 26 树的子结构

class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        boolean result=false;
        if(A!=null&&B!=null){
            if(A.val==B.val){
                result=isTrue(A,B);
            }
            //result是标杆
            if(!result){
                result=isSubStructure(A.left,B);
            }
            if(!result){
                result=isSubStructure(A.right,B);
            }
        }
        return result;
    }
    public boolean isTrue(TreeNode A,TreeNode B){
        //要先判断B,因为B才是正确与否的标准
        if(B==null){
            return true;
        }
        if(A==null){
            return false;
        }
        if(A.val!=B.val){
            return false;
        }
        return isTrue(A.left,B.left)&&isTrue(A.right,B.right);
    }
}
posted @ 2022-03-12 09:26  Dreamer_szy  阅读(12)  评论(0)    收藏  举报