题解 | #对称的二叉树#

对称的二叉树

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

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function isSymmetrical(pRoot) {
    // write code here
    if (!pRoot) return true;
    function compare(node1, node2) {
        if (!node1 && !node2) return true;
        if (!node1 || !node2) return false;
        if (node1.val !== node2.val) {
            return false;
        } else {
            // 通过比较是否相等来判断,必须是两个条件同时满足才可
            return (
                compare(node1.left, node2.right) &&
                compare(node1.right, node2.left)
            );
        }
    }
    return compare(pRoot.left, pRoot.right);
}
module.exports = {
    isSymmetrical: isSymmetrical,
};

全部评论

相关推荐

牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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