题解 | #对称的二叉树#

对称的二叉树

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 === null) return true
    if (pRoot.left === null && pRoot.right === null) {
        return true
    } else {
        const nodeArray = [pRoot.left, pRoot.right]
        while (nodeArray.length > 0) {
            const head = nodeArray.shift()
            const tail = nodeArray.pop()
            if (head === null && tail === null) {
                continue
            } else if (head !== null && tail !== null && head.val === tail.val) {
                nodeArray.unshift(head.right)
                nodeArray.unshift(head.left)
                nodeArray.push(tail.left)
                nodeArray.push(tail.right)
            } else {
                return false
            }
        }
        return true
    }
}
module.exports = {
    isSymmetrical : isSymmetrical
};

全部评论

相关推荐

机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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