题目

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. Matlab 也很强大!

    一.实时编辑器 所创建的脚本不仅可以捕获代码,还可以讲述与人分享的故事.自动化的上下文提示可让您在编程时快速推进,并且将结果与可视化内容和您的代码一起显示. 一般以 .mlx 为后缀. 二.App D ...

  2. Counter Strike HDU 2443 逆序对数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2443 这个题目尝试了很多种方法都过不去,上网查了一下网友们的的思路,竟然和逆序对数有关系!! 题目大意: ...

  3. WebApi入门

    饮水思源 http://www.cnblogs.com/guyun/p/4589115.html http://www.cnblogs.com/chutianshu1981/p/3288796.htm ...

  4. web服务端安全之SQL注入攻击

    一.SQL注入攻击的原理攻击者在HTTP请求中,注入恶意的SQL代码,并在服务端执行.比如用户登录,输入用户名camille,密码 ' or '1'='1 ,如果此时使用参数构造的方式,就会出现 ' ...

  5. django 动态url 可变

    首先在urls里面改,name=让一个映射敷个名字. 然后到books——list页面让编辑按钮改成这种可变的映射模式.

  6. hihocoder 1526 序列的值

    题面在这里! 好久没做题啦,养生一下qwq 推一推贡献就会发现这TM就是一个水题啊! #include<bits/stdc++.h> #define ll long long using ...

  7. Go语言特点

    作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...

  8. 在线HTTP速度测试(响应时间测试)及浏览器兼容测试

    一.前言 网站的响应时间,是判断一个网站是否是好网站的重要的因素之一.百度首页的响应时间在全国各个省份小于10ms.这个响应时间远远好于竞争对手.根据美丽说的技术负责人分析,美丽说访问速度提升10%, ...

  9. kali下利用weeman进行网页钓鱼

    工具下载链接:https://files.cnblogs.com/files/wh4am1/weeman-master.zip 利用wget命令下载zip压缩包 利用unzip命令解压 接着直接cd进 ...

  10. python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)

    在上一篇blog:python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 中介绍了python中的tkinter的一些东西,你可能对tkinter有一定的了解了.这篇b ...