先判断是否相等,再遍历,看是否包含
class Solution {
//判断A是否与B相等
public boolean issame(TreeNode A, TreeNode B){
if(B==null){
return true;
}
if(A==null||A.val!=B.val){
return false;
}
return issame(A.left,B.left)&&issame(A.right,B.right);
}
//遍历A
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A==null||B==null) return false;
return issame(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
}