Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 5x 1x 4x 4x 4x 10x 10x 10x 4x 6x 2x 6x 5x 6x | import type { TreeNode } from './Tree'
export default function minDepth(
root: TreeNode<number> | null,
): number | undefined {
if (root === null)
return 0
let depth = 1
const queue = [root]
while (queue.length > 0) {
for (let i = 0, size = queue.length; i < size; i++) {
const node = queue.shift() as TreeNode<number>
if (node.left === null && node.right === null)
return depth
if (node.left)
queue.push(node.left)
if (node.right)
queue.push(node.right)
}
depth++
}
}
|