二叉树最小深度

求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。

/*
 * 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
};
树算法 文章被收录于专栏

树相关算法

全部评论

相关推荐

点赞 评论 收藏
分享
01-24 08:13
已编辑
合肥工业大学 Java
程序员牛肉:没啥问题。标准的流水线简历,但是学历好一点,所以应该是有约面的机会的。 这段时间可以考虑把自己的两个项目彻底的理一理。争取能够讲清楚每一个功能点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务