题解 | #树的子结构#
树的子结构
https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88
public class Solution {
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
if (root2 == null)return false;
if (root1 == null)return false;
boolean flag = isLike(root1, root2);
boolean next = HasSubtree(root1.left, root2);
boolean next2 = HasSubtree(root1.right, root2);
return flag || next || next2;
}
public boolean isLike(TreeNode a, TreeNode b) {
if (b == null)return true;
if (a == null || a.val != b.val)return false;
return isLike(a.left, b.left) && isLike(a.right, b.right);
}
}