题目

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.

题解

递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth。

递归解法:

     public int minDepth(TreeNode root) {
         if(root == null)
             return 0;
         int minleft = minDepth(root.left);
         int minright = minDepth(root.right);
         if(minleft==0 || minright==0)
             return minleft>=minright?minleft+1:minright+1;
         return Math.min(minleft,minright)+1;
     }

非递归解法:

 1     public int minDepth(TreeNode root) {
 2         if(root == null)
 3             return 0;
 4         
 5         int depth = 1;//The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
 6         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
 7         queue.add(root);
 8         int curnum = 1;
 9         int nextnum = 0;
         while(!queue.isEmpty()){
             TreeNode cur = queue.poll();
             curnum--;
             
             if(cur.left == null && cur.right == null)
                 return depth;
             
             if(cur.left != null){
                queue.add(cur.left);
                nextnum++;
             }
             
             if(cur.right != null){
                 queue.add(cur.right);
                 nextnum++;
             }
             
             if(curnum == 0){
                 curnum = nextnum;
                 nextnum = 0;
                 depth++;
             }
         }
         return depth;
     }

Minimum Depth of Binary Tree leetcode java的更多相关文章

  1. Minimum Depth of Binary Tree ——LeetCode

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. Minimum Depth of Binary Tree [LeetCode]

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  4. LeetCode算法题-Minimum Depth of Binary Tree(Java实现)

    这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  9. LeetCode: Minimum Depth of Binary Tree 解题报告

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. redis在Linux下的远程连接

    1.redis在Linux下的远程连接: $ redis-cli -h host -p port -a password 如何连接到主机为 127.0.0.1,端口为 6379 ,密码为 mypass ...

  2. GPS数据包格式及数据包解析

    GPS数据包解析 GPS数据包解析 目的 GPS数据类型及格式 数据格式 数据解释 解析代码 结构体定义 GPRMC解析函数 GPGGA解析函数 测试样例输出 gps数据包格式 gps数据解析 车联网 ...

  3. 装饰模式和Java IO

    装饰模式 修饰模式(装饰模式),是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式.就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能. 装饰模式的UM ...

  4. MySQL大事务导致的Insert慢的案例分析

    [问题] 有台MySQL服务器不定时的会出现并发线程的告警,从记录信息来看,有大量insert的慢查询,执行几十秒,等待flushing log,状态query end [初步分析] 从等待资源来看, ...

  5. Java 集合Collection——初学者参考,高手慎入(未完待续)

    1.集合简介和例子 Collection,集合.和数学定义中的集合类似,把很多元素放在一个容器中,方便我们存放结果/查找等操作. Collection集合实际上是很多形式集合的一个抽象. 例如十九大就 ...

  6. JavaSE基础之double数据类型的格式化

    JavaSE基础之double数据类型的格式化 1.double 数据类型的格式化工具类:DoubleFormatUtil.java package cn.com.zfc.util; import j ...

  7. [51Nod 1773] A国的贸易

    [51Nod 1773] A国的贸易 题目描述 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们有着神奇的贸易规则. ...

  8. [CC-XYHUMOQ]A humongous Query

    [CC-XYHUMOQ]A humongous Query 题目大意: 有一个长度为\(n(n\le32)\)的以\(1\)开头,\(0\)结尾的\(01\)序列\(S\).令\(f(S)\)表示序列 ...

  9. 轻巧的编辑器:Sublime Text3 user设置

    开发到现在,编辑器倒用过不少,VIM.zend.my eclipse.EPP.editplus.notepad++.sublime text 2. 最初使用sublime是同学推荐的,说其何其的好,何 ...

  10. BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树

    4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...