题解 | #对称的二叉树#

对称的二叉树

http://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;
  const queue = [];
  queue.push(pRoot);
  queue.push(pRoot);

  while (queue.length) {
    const root1 = queue.shift();
    const root2 = queue.shift();
    if (!root1 && !root2) continue;
    if (!root1 || !root2 || root1.val !== root2.val) {
      return false;
    }
    queue.push(root1.right);
    queue.push(root2.left);

    queue.push(root1.left);
    queue.push(root2.right);
  }
  return true;

  //   // 递归解法
  //   if (pRoot == null) return true;
  //   const DFS = (root1, root2) => {
  //     if (!root1 && !root2) return true;
  //     if (!root1 || !root2 || root1.val !== root2.val) {
  //       return false;
  //     }
  //     return DFS(root1.left, root2.right) && DFS(root1.right, root2.left);
  //   };
  //   return DFS(pRoot, pRoot);
}
module.exports = {
  isSymmetrical: isSymmetrical,
};

全部评论

相关推荐

书海为家:实习是成为大厂正式员工很好的敲门砖,看您的简历中有一段实习经历,挺好的。我来给一点点小建议,因为毕竟还在学校不像工作几年的老鸟有丰富的项目经验,面试官在面试在校生的时候更关注咱们同学的做事逻辑和思路,所以最好在简历中描述下自己实习时做过项目的完整过程,比如需求怎么来的,你对需求的解读,你想到的解决办法,遇到困难如何找人求助,最终项目做成了什么程度,你从中收获了哪些技能,你有什么感悟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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