题解 | #对称的二叉树#

对称的二叉树

http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb

请学会妙用check函数 使用arr的话 同样也是这个思想 根左右与跟右左

    this.val = x;
    this.left = null;
    this.right = null;
} */
function isSymmetrical(pRoot)
{
    // write code here
    // 队列想到了
    
    //怎么这种递归都要搞个check函数啊 
    if(!pRoot) return true
    return check(pRoot.left,pRoot.right)
    
  

    
}
function check(a,b){
    if(!a&&!b) return true
    if(a&&!b) return false
    if(!a&&b) return false
    if(a.val===b.val) return check(a.left,b.right)&&check(a.right,b.left)
}
module.exports = {
    isSymmetrical : isSymmetrical
};
全部评论
与那道中等题 二叉树的子树有异曲同工之妙 那道只是增加了一个左孩子或者右孩子与子树相等
点赞 回复 分享
发布于 2022-04-28 16:26

相关推荐

认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务