两棵树是否相同
public static boolean isSameTree(Node p,Node q){
if (p == null && q == null){
return true;
}
if (p == null || q == null) {
return false;
}return p.value == q.value
&& isSameTree(p.left,q.left)
&& isSameTree(p.right,q.right);
}
两棵树是否镜像
public static boolean isMirror(Node p,Node q){
if (p == null && q == null){
return true;
}
if (p == null || q == null) {
return false;
}return p.value == q.value
&& isMirror(p.left,q.right)
&& isMirror(p.right,q.left);
}
一棵树是否对称
//子树互为镜像的
public static boolean isSymmetric(Node root){
if (root == null){return true;}
return isMirror(root.left,root.right);
}
一棵树是否为另一棵树的子树
//搜索子树
public static boolean search(Node root,Node t){
if (root == null){return false;}
if (root.value == t.value){
if (isSameTree(root,t)){
return true;
}
}
if (search(root.left,t)){
return true;
}
return search(root.right,t);
}
public static boolean isSubtree(Node s,Node t){
return search(s,t);
}