LC 101. Symmetric Tree

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
  if (root === null) return true
  let queue = []
  queue.push(root.left)
  queue.push(root.right)
  while (queue.length) {
    let leftNode = queue.shift()
    let rightNode = queue.shift()
    if (leftNode === null && rightNode === null) continue
    if (leftNode === null || rightNode === null || leftNode.val !== rightNode.val) return false
    queue.push(leftNode.left)
    queue.push(rightNode.right)
    queue.push(leftNode.right)
    queue.push(rightNode.left)
  }
  return true
};
欢迎通过「邮件」或者点击「这里」告诉我你的想法
Welcome to tell me your thoughts via "email" or click "here"