描述
给定一棵二叉树,判断其是否是自身的镜像(即:是否对称)
基本思路
先单独写一个函数对比两棵树是否相同,然后将树的左右节点传进去进行比较
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function isSymmetrical(pRoot)
{
// write code here
if(!pRoot) return true
function compare(p1,p2){
if(!p1&&!p2) return true
if(!p1||!p2) return false
return (p1.val === p2.val)&&compare(p1.left,p2.right)&&compare(p1.right,p2.left)
}
return compare(pRoot.left,pRoot.right)
}
module.exports = {
isSymmetrical : isSymmetrical
};