Skip to content

Commit

Permalink
feat: 二叉树的最小深度
Browse files Browse the repository at this point in the history
  • Loading branch information
sl1673495 committed Jun 9, 2020
1 parent 54f5edf commit 1da09c9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 二叉树/二叉树的最小深度-111.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let minDepth = function (root) {
if (!root) return 0

let depth = 0
let queue = [root]

while (queue.length) {
depth++
let len = queue.length
while (len--) {
let node = queue.shift()

let left = node.left
let right = node.right
if (!left && !right) {
return depth
}

if (left) {
queue.push(left)
}
if (right) {
queue.push(right)
}
}
}
}

0 comments on commit 1da09c9

Please sign in to comment.