Title Description
Two binary inputs A, B, B is judged not substructure A's. (Ps: we agreed empty tree is not a tree any substructure)
AC Code
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
//如果是空树,返回false
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null||root2==null)
return false;
if(isPart(root1,root2))
return true;
return HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
}
public boolean isPart(TreeNode r1,TreeNode r2){
if(r2==null)
return true;
if(r1==null||(r1.val!=r2.val))
return false;
return isPart(r1.left,r2.left)&&isPart(r1.right,r2.right);
}
}