1.判断两颗树相等
public boolean isEqual(TreeNode t1,TreeNode t2){
if(t1 == null && t2 == null){
return true;
}
if(t1 == null && t2 != null || t1 != null && t2 == null){
return false;
}
if(t1.val != t2.val){
return false;
}else{
boolean left = isEqual(t1.left,t2.left);
boolean right = isEqual(t1.right,t2.right);
return (left&&right);
}
}
2、判断子树
public boolean isSubtree(TreeNode T1,TreeNode T2){
if(T2 == null){
return true;
}
if(T1 == null && T2 != null){
return false;
}
if(isEqual(T1,T2)){
return true;
}
if(T1.left != null){
boolean left = isSubtree(T1.left,T2);
if(left) {
return true;
}
}
if(T1.right != null){
boolean right = isSubtree(T1.right,T2);
if(right){
return true;
}
}
return false;
}