题解 | #对称的二叉树#

对称的二叉树

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

/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pRoot TreeNode类
 * @return bool布尔型
 */
function compare(a, b) {
    if (a === null && b === null) return true;
    if (a === null || b === null) return false;
    if (a.val!==b.val) return false;
    return compare(a.left,b.right)&&compare(a.right,b.left)
}

function isSymmetrical(pRoot) {
    if (pRoot === null) return true;
    return compare(pRoot.left,pRoot.right)
}
module.exports = {
    isSymmetrical: isSymmetrical,
};

全部评论

相关推荐

喜欢飞来飞去的雪碧在...:可以试一试字节
点赞 评论 收藏
分享
点赞 评论 收藏
分享
明天不下雨了_人机版:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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