求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @return int整型
*/
function run( root ) {
// write code here
if(!root) {return 0}
const queue = [root]
let depth = 0
let len = 0
while(len = queue.length){
depth++
for(let i=0;i<len;i++){
const node = queue.shift()
if(!node.left&&!node.right) return depth;
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
}
}
}
module.exports = {
run : run
};
京公网安备 11010502036488号