# BFS 算法解题套路框架
问题的本质就是让你在一幅「图」中找到从起点 start 到终点 target 的最近距离,这个例子听起来很枯燥,但是 BFS 算法问题其实都是在干这个事儿
/**
* 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 {number}
*/
var minDepth = function (root) {
// bfs
if (!root) return 0
const queue = []
let res = 1
queue.push(root)
while (queue.length) {
const sz = queue.length
for (let i = 0; i < sz; i++) {
let cur = queue.shift()
if (cur.left === null && cur.right === null) {
return res
}
if (cur.left) {
queue.push(cur.left)
}
if (cur.right) {
queue.push(cur.right)
}
}
res += 1
}
}