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.

这个题跟求最大深度差不多。

使用递归最简单。不使用递归就是在层序遍历的时候判断。。见代码

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
/* if(root.left==null&&root.right==null) return 1;
if(root.left==null) return minDepth(root.right)+1;
if(root.right==null) return minDepth(root.left)+1;
int left=minDepth(root.left);
int right=minDepth(root.right);
return Math.min(left,right)+1;
*/
Queue<TreeNode> q=new LinkedList<>();
q.add(root);
int count=1;
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode node=q.poll();
if(node.left==null&&node.right==null) return count;
if(node.left!=null) q.add(node.left);
if(node.right!=null) q.add(node.right);
}
count++;
}
return count;
}
}

minimun depth of binary tree的更多相关文章

  1. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

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

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  3. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  4. [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 ...

  5. 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 ...

  6. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

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

随机推荐

  1. android EventBus 3.0使用指南

    Enventbus的作用和好处我就不多说了,这里介绍下怎么使用. 2.+版本的使用方法 public void onEvent(MessageEvent event) { log(event.mess ...

  2. 04 Spinner 列表选中

    <span style="font-size:18px;"> <?xml version="1.0" encoding="utf-8 ...

  3. 自己动手写hibernate

    这篇文章 可作为北京尚学堂 hibernate的学习笔记 再学习hibernate之前 得有一点反射的基础知识 package com.bjsxt.hibernate; public class St ...

  4. C算法分解质因数与分解因子

    ) ) printf("%d ",i); } }

  5. Android用AlarmManager实现后台任务-android学习之旅(63)

    因为Timer不能唤醒cpu,所以会在省电的原因下失效,所以需要唤醒cpu在后台稳定化的执行任务,AlarmManager能够唤醒cpu 这个例子讲解了如何通过Service来在后他每一个小时执行.特 ...

  6. pig的limit无效(返回所有记录)sample有效

    pig中,limit可以取样少部分数据,但有很多问题,比如数据不能少于10条,否则返回全部. 今天又遇到另一个问题: group后的数据,limit无效:也就是group后的数据,不能用limit,估 ...

  7. 使用LogKit进行日志操作

    1.      概述 任何一个系统中,日志都是不可缺少的,现在Apache提供了两套日志工具,一个就是Log4j,另一个是本文要给出例子的LogKit. Log4j和LogKit有很多相似的地方.比如 ...

  8. 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景

    ============================================================================ 服务的生命周期: 一.采用start的方式开始 ...

  9. Ext JS 5初探(一)

    在开始前,先安装好Sencha Cmd 5.然后输入以下命令创建一个测试用的应用程序: sencha -sdk c:\ext5 generate app TestExt5 C:\TestExt5 想不 ...

  10. OpenCV——颜色运算(二)

    #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include <iostream> #include & ...