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
}
if(!compTree(pRoot1,pRoot2)){
//遍历A树
return HasSubtree(pRoot1.left,pRoot2) || HasSubtree(pRoot1.right,pRoot2)
}
return true
//比较两棵树是否相同
function compTree(root1,root2){
if(!root2){
return true
}
if(!root1){
return false
}
if(root1.val !== root2.val){
return false
}
return compTree(root1.left,root2.left) && compTree(root1.right,root2.right)
}
}
module.exports = {
HasSubtree : HasSubtree
};