题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
js代码
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function HasSubtree(pRoot1, pRoot2)
{
// write code here
if (!pRoot1 || !pRoot2) return false
return isSubtree(pRoot1, pRoot2) || HasSubtree(pRoot1.left, pRoot2) || HasSubtree(pRoot1.right, pRoot2)
}
function isSubtree(p, q){
if (!q) return true
if (!p) return false
if (p.val === q.val){
return isSubtree(p.left, q.left) && isSubtree(p.right, q.right)
}else return false
}