Skip to content

maximum-depth-of-binary-tree 二叉树的最大深度

开始以为传入的数据是示例数据 [3, 9, 20, null, null, 15, 7],写出了以下版本:

javascript
/**
 * @param {(number|null)[]} root
 */
const binaryMaxDepth = (root) => {
  let pastNodeCount = 0
  let level = 0
  let nodeCount = Math.pow(2, level)
  while (root.slice(pastNodeCount, pastNodeCount + nodeCount).some((node) => node !== null)) {
    pastNodeCount += nodeCount
    level++
    nodeCount = Math.pow(2, level)
  }
  return level + 1
}

有以下改进的地方:

  • 使用了 slice 占用了