minimun 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.
这个题跟求最大深度差不多。
使用递归最简单。不使用递归就是在层序遍历的时候判断。。见代码
/**
* 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
随机推荐
- 消息字节——MessageBytes
在tomcat核心处理中有这么一个需求--"为了提高编码性能,对于socket接收到的字节流不马上进行某种编码的转码,而是应该保留字节流的形式,在需要时.在指定编码时才进行转码工作" ...
- IDEA中运行KafkaWordCount程序
1,从spark的example中找到KafkaWordCount.scala文件复制到idea编辑器中,引入包: 2,编辑configuration, (1)KafkaWordCountPr ...
- Ubuntu中firefox设置成中文
进入 http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly 按版本选择下去,帮助(help)-->关于,查看浏览器的版本号 所以,目录是3 ...
- 数值分析:Hermite多项式
http://blog.csdn.net/pipisorry/article/details/49366047 Hermite埃尔米特多项式 在数学中,埃尔米特多项式是一种经典的正交多项式族,得名于法 ...
- 【一天一道LeetCode】#95. Unique Binary Search Trees II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 12.3、Libgdx的图像之截屏
(官网:www.libgdx.cn) 接下来的示例说明怎样进行截屏并且保存为PNG格式. public class ScreenshotFactory { private static int cou ...
- 深入 JAVA里面关于byte数组和String之间的转换问题
把byte转化成string,必须经过编码. 例如下面一个例子: importjava.io.UnsupportedEncodingException; publicclass test{ pub ...
- 学习pthreads,使用条件变量进行多线程之间的同步
条件变量提供另一种多线程同步的方法.互斥量通过控制对共享数据的访问来同步任务.条件变量可以根据数据的值来同步任务.条件变量是当一个事件发生时发送信号的信号量.一旦事件发生,可能会有多个线程在等待信号, ...
- Android官方技术文档翻译——Gradle 插件用户指南(4)
最近赶项目,白天基本没时间,只有晚上在家的时候才能看一看.昨天晚上只翻译完了第四章,今天就只发第四章吧. 本文译自Android官方技术文档<Gradle Plugin User Guide&g ...
- FSM之SMC使用总结
FSM之SMC使用总结 Part1: Smc.jar state machine compiler usage Reference: http://smc.sourceforge.net/ ...