
public static boolean isSubTree(BinaryTreeNode root1,BinaryTreeNode root2){
if(root2 == null){
return true;
}
if(root1 == null){//root1==null root2!=null
return false;
}
return root1.data == root2.data && isSubTree(root1.left,root2.left) && isSubTree(root1.right,root2.right);
}
public static boolean HasSubtree(BinaryTreeNode root1,BinaryTreeNode root2) {
if(root1 == null || root2 == null){
return false;
}
if(isSubTree(root1,root2)){
return true;
}
return HasSubtree(root1.left,root2) || HasSubtree(root1.right,root2);
}
近期评论