AcWing 37. 树的子结构
原题链接
简单
作者:
LangB
,
2020-11-09 12:27:14
,
所有人可见
,
阅读 304
AcWing 37. 树的子结构
class Solution {
public boolean hasSubtree(TreeNode pRoot1, TreeNode pRoot2) {
if (pRoot1 == null || pRoot2 == null) {
return false;
}
if (isPart(pRoot1, pRoot2)) {
return true;
}
return hasSubtree(pRoot1.left, pRoot2) || hasSubtree(pRoot1.right, pRoot2);
}
private boolean isPart(TreeNode pRoot1, TreeNode pRoot2) {
if (pRoot2 == null) {
return true;
}
if (pRoot1 == null || pRoot1.val != pRoot2.val) {
return false;
}
return isPart(pRoot1.left, pRoot2.left) && isPart(pRoot1.right, pRoot2.right);
}
}