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 ...
随机推荐
- 关于React Native项目在android上UI性能调试实践
我们尽最大的努力来争取使UI组件的性能如丝般顺滑,但有的时候这根本不可能做到.要知道,Android有超过一万种不同型号的手机,而在框架底层进行软件渲染的时候是统一处理的,这意味着你没办法像iOS那样 ...
- Socket实现聊天客户端
今天在极客学院上看到了一个关于Socket的视频讲解,感觉还不错,就写了份代码,拿来分享一下. Socket使用方法 关于Socket的使用,我们首先要弄清楚的是,在服务器端还是在客户端使用.因为这的 ...
- 解决ActionBar中的item不显示在ActionBar的问题
今天在用ActionBar,需要增加一个菜单选项,按教程在/res/menu下对应的布局文件中添加了一个item,但是它却是显示在overflow中,而不是直接显示在ActionBar当中的.我的布局 ...
- Java-IO之RandomAccessFile
RandomAccessFile是随机访问(读写)的类,支持对文件随机访问的读取和写入,也可以从指定的位置读取和写入文件数据.RandomAccessFile虽然属于java.io包,但它不是Inpu ...
- smack4中文文档
smack4中文文档 基于samck官方最新文档翻译而成,适用于最新的Smack4.x 简介 6月毕业后来到帝都上班,找了一份Android开发的工作,公司开发的APP需要使用XMPP和Smack进行 ...
- Windows7下使用mingw编译openssl
Windows7下使用mingw编译openssl 首先参考这篇文章安装mingw/minsys: http://blog.csdn.net/ubuntu64fan/article/details/8 ...
- Linux下修改主机名步骤
Linux下修改主机名为gpdb 步骤一.运行vi /etc/sysconfig/network命令 NETWORKING=yesHOSTNAME=gpdb 步骤二.运行hostname gpdb命令 ...
- 深入解析Linux中的fork函数
1.定义 #include <unistd.h> #include<sys/types.h> pid_t fork( void ); pid_t 是一个宏定义,其实质是int, ...
- Android Studio 从安装到配置使用
Android Studio是谷歌为android量身定制的IDE,在2013年谷歌大会上提出之后一直持续更新,现在已经是功能十分强劲的android开发工具,作为一个android开发者,还是早点转 ...
- AngularJS进阶(三十四)Angular数据更新不及时问题探讨
Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ...