当前位置: 首页 > news >正文

LeetCode -- Minimum Depth of Binary Tree

题目描述:


Given a binary tree, find its minimum depth.


The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.


就是求一个树的最小层数。


思路:
从根节点进行BFS ,找到第一个叶子节点即可。


实现代码:




/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int MinDepth(TreeNode root) 
    {
        if(root == null){
            return 0;
        }
        
        var layer = 1;
        var nodes = new List<TreeNode>();
        LoadChildren(ref nodes, root);
        if(nodes.Count == 0){
            return layer;
        }
        
        layer ++;
        
        MinDepth(nodes,ref layer);
        return layer;
    }
    private void MinDepth(List<TreeNode> parents, ref int layer){
        var next = new List<TreeNode>();
        for(var i = 0;i < parents.Count; i++){
            if(parents[i].left == null && parents[i].right == null){
                return;
            }
            if(parents[i].left != null){
                next.Add(parents[i].left);
            }
            if(parents[i].right != null){
                next.Add(parents[i].right);
            }
        }
        
        layer ++;
        MinDepth(next,ref layer);
    }
    private void LoadChildren(ref List<TreeNode> nodes, TreeNode node){
        if(node.left != null){
            nodes.Add(node.left);
        }
        if(node.right != null){
            nodes.Add(node.right);
        }
    }
}


相关文章:

  • ArcGIS Server Java ADF 案例教程 26
  • LeetCode -- Minimum Path Sum
  • LeetCode -- Multiply Strings
  • ArcGIS Server Java ADF 案例教程 27
  • LeetCode -- Permutations II
  • SQL语句性能调整之ORACLE的执行计划
  • LeetCode -- Product of Array Except Self
  • 不知道为什么我的一oracle的sql调优文章笔记无法发表,提示“文章中出现禁止的词语,系统不予接受。”...
  • LeetCode -- Remove Duplicates From Sorted Array 2
  • 好人陈虻
  • LeetCode -- Reverse Bits
  • LeetCode -- Rotate Array
  • SQL2005CLR函数扩展-天气服务
  • LeetCode -- String to Integer (atoi)
  • JavaScript 读写文件
  • 【附node操作实例】redis简明入门系列—字符串类型
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Android框架之Volley
  • CentOS7简单部署NFS
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • Javascript 原型链
  • JavaScript学习总结——原型
  • mysql中InnoDB引擎中页的概念
  • tab.js分享及浏览器兼容性问题汇总
  • 从伪并行的 Python 多线程说起
  • 浮现式设计
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 前端自动化解决方案
  • 区块链分支循环
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • ​2020 年大前端技术趋势解读
  • (11)MSP430F5529 定时器B
  • (Java)【深基9.例1】选举学生会
  • (poj1.2.1)1970(筛选法模拟)
  • (pytorch进阶之路)扩散概率模型
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (理论篇)httpmoudle和httphandler一览
  • (深入.Net平台的软件系统分层开发).第一章.上机练习.20170424
  • (十)DDRC架构组成、效率Efficiency及功能实现
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)fock函数详解
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转载)利用webkit抓取动态网页和链接
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • .Mobi域名介绍
  • .NET 5种线程安全集合
  • .NET Core 中的路径问题
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • ?php echo ?,?php echo Hello world!;?
  • @media screen 针对不同移动设备
  • [《百万宝贝》观后]To be or not to be?
  • [Android开源]EasySharedPreferences:优雅的进行SharedPreferences数据存储操作
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作