LeetCode(37)-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.
思路:
- 题意是求一颗二叉树的的最短路径
- 思路可以参考求二叉树的深度的算法,还是用递归的思想,考虑上级节点和下级左右子树的关系
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.left != null && root.right == null){
return 1+minDepth(root.left);
}else if(root.left == null && root.right != null){
return 1+minDepth(root.right);
}else{
return 1+Math.min(minDepth(root.left),minDepth(root.right));
}
}
}
LeetCode(37)-Minimum Depth of Binary Tree的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [LeetCode] 111. 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][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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode 111. 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 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
随机推荐
- android自定义View-继承
介绍anroid通过继承系统的控件自定义view 方法是通过对OnDraw()方法进行复写来实现的 举例继承TextView 在textView的背景加上矩形的效果 代码实现 testView的代码 ...
- Maven创建EJB
开发工具: eclipse mars wildfly jdk8 maven 右键新建project,选择other 勾选create simple project 填写信息(自行填写),完成后右键项目 ...
- C语言省略extern的缺陷
在一个文件中(比如a.c)定义一个全局变量int a = 10; 然后在另一个代码文件(比如main.c)中需要使用变量a,可以写 int a; 单独看main.c文件时就会出现二义性,一个含义是当其 ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...
- 开发Nginx模块Helloworld
本文是对<深入理解Nginx>一书中的实例进行实战时的记录. 1模块目录结构 my_test_module/ ├── config └── ngx_http_mytest_module.c ...
- Tapestry: Obtained resource by @Inject is NULL
Issue: When you inject some resources by @Inject Annotation in Tapestry Page or other components, yo ...
- Android:ADB server didn't ACK或者adb server is out of date. killing解决办法
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 出现这个原因我个人感觉有两个.一.5037端口被别的程序或者进程占用:二.adb占用的不是5037端口.很多人仅仅知道第一种二忽 ...
- [Mac] mac linux 多线程下载利器 axel
> 之前做过一些文件下载的统计,发现谷歌浏览器chrome和火狐firefox, 一般都是单线程的下载文件,360浏览器却是多线程的下载. 现在切换到了mac上,发现没有360哪个浏览器,就像 ...
- Dynamics CRM2013 去除界面顶部黄色的CRM For Outlook条框
Dynamics CRM2013中每次打开系统页面上方都会有个黄条看着很是烦人,效果如下图 庆幸的是系统提供了关闭的开关,设置-管理-系统设置,把"设置CRM For Outlook消息是否 ...
- HTML5 Web Storage 特性
原文地址: Using HTML5 Web Storage 原文日期: 2010年06月28日 翻译日期: 2013年08月12日 当下Web开发领域最火爆的词语当属 HTML5.HTML5标准的新特 ...