Java [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 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) return 1 + minDepth(root.right);
if(root.right == null) return 1 + minDepth(root.left);
return Math.min(1 + minDepth(root.left), 1 + minDepth(root.right));
}
}
Java [Leetcode 111]Minimum Depth of Binary Tree的更多相关文章
- [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 ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- Java for 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 ----- java
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java实现LeetCode 111. Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- 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 ...
- (二叉树 BFS DFS) 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 ...
随机推荐
- Ubuntu C++环境支持
问题描述: 在Ubuntu中默认安装有gcc,但是只能编辑C程序,现在希望添加C++环境支持 问题解决: 首先是配置gcc,在ubuntu安装完成已经有gcc了(gcc ...
- Jsonp 前后端交互操作
今天,因为项目的需要,研究了一下JSONP,特在此记录一下 ,希望可以帮助那些有疑惑的朋友们,本人也是刚学,高手略过即可. 关于Jsonp的定义就不说了,网上一片,大家可以自己查询.我就在此直接进入正 ...
- PHP之preg_replace()与ereg_replace()正则匹配比较讲解
<?php //preg_replace()和ereg_replace()函数的使用的比较 // -------preg_replace()-------------------------- ...
- 如何使用 Apache ab 以及 OneAPM 进行压力测试?
下一个 release 准备小长假后就要 go-live ,所有的测试 case 都 cover 过了,但还未进行过压力测试,有点不放心,刚好过节期间家人都回家去了,假期终于可以抽点时间压测一把. A ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- Chp18: Hard
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. Solut ...
- hdu1233
http://acm.hdu.edu.cn/showproblem.php?pid=1233 最小生成树,kruskal算法 #include<stdio.h> #include<m ...
- java split IP地址要用双斜杠
示例代码: public void test() { String address = "11.12.13.14:800"; System.out.println(address. ...
- flexbox弹性盒子布局
混合划分 demo1,css: #demo1{ width: 100%; background: #ccc; display: -webkit-flex;/*表示使用弹性布局*/ } #demo1 . ...
- Sina App Engine(SAE)入门教程(8)- SaeFetchurl使用
fetchurl是什么? FetchURL是SAE为开发者提供的分布式网页抓取服务,用来同步的抓取http页面,FetchURL针对国内的网络的做了优化,内部有调度系统,尽可能保证用户快速的抓取到目标 ...