1、let root = [3,9,20,null,null,15,7]; // 二叉树最小深度 bfs
var minDepth = function(root) {
if (!root) {
return 0;
}
let deep = 1;
let queue = [root];
while (queue.length) {
let sz = queue.length;
for (let i = 0; i < sz; i++) {
let cur = queue.shift();
if (!cur.left && !cur.right) {
return deep;
}
if (cur.left) {
queue.push(cur.left);
}
if (cur.right) {
queue.push(cur.right);
}
}
deep++;
}
};
网友评论