Lintcode155-Minimum Depth of Binary Tree-Easy
155. 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.
Example
Example 1:
Input: {}
Output: 0
Example 2:
Input: {1,#,2,3}
Output: 3
Explanation:
1
\
2
/
3
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: The root of binary tree
* @return: An integer
*/
int minDepth = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right != null) {
helper(root.right, 2);
} else if (root.right == null && root.left != null) {
helper(root.left, 2);
} else {
helper(root, 1);
}
return minDepth;
}
public void helper(TreeNode root, int curDepth) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
if (curDepth < minDepth) {
minDepth = curDepth;
}
return;
} helper(root.left, curDepth + 1);
helper(root.right, curDepth + 1);
}
}
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
return getMin(root);
}
public int getMin(TreeNode root){
if (root == null) {
return Integer.MAX_VALUE;
}
if (root.left == null && root.right == null) {
return 1;
}
return Math.min(getMin(root.left), getMin(root.right)) + 1;
}
}
Lintcode155-Minimum Depth of Binary Tree-Easy的更多相关文章
- 【leetcode】Minimum Depth of Binary Tree (easy)
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
111. Minimum Depth of Binary Tree Easy Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...
- [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 ...
- 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练习题】Minimum 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 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
随机推荐
- U盘挂载指令
相关步骤来自于百度 1.先确定U盘格式,如果是ntfs格式,需要先安装3g-ntfs这个软件支持ntfs格式,以下默认U盘为fat32格式 2.使用fdisk -l 查看分区表,找到u盘的设备号,比如 ...
- numpy(三)
广播: x= np.arange(12).reshape((3,4)) a= np.arange(3) b=np.arange(3)[;,np.newaxis] c=a+b a,b会扩散成公共的形状进 ...
- SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合
SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...
- C# .net 语言加密方案
C# .net 语言加密方案 方案背景 当前C# .net语言的应用范围越来越广泛,IIS 的服务器架构后台代码.桌面应用程序的 winform .Unity3d 的逻辑脚本都在使用.C# .net ...
- Opencv-Python No module named 'cv2.cv2'
关于 No module named 'cv2.cv2'等其他一些问题,一般都是版本不兼容的问题,重装即可. pip uninstall opencv-python 然后 pip install op ...
- iOS MJExtension的使用
前言: MJExtension是iOS的字典装模型的一个第三方框架.相对于JSONKit和SBJson相比MJExtension更简单易用.功能更强大. 安装: 使用CocoaPods导入(Cocoa ...
- ionic3 小记录
cordova platform add ios@latest 安装最新ios ionic cordova build ios -- --buildFlag="-UseModernBuild ...
- 使用Epplus生成Excel 图表
1. 前言 这是我最近项目刚要的需求,然后在网上找了半天的教材 但是很不幸,有关于Epplus的介绍真的太少了,然后经过了我的不断研究然后不断的采坑,知道现在看到Excel都想吐的时候,终于成功的 ...
- 关于mysql分组查询
在mysql查询中,用到GROUP BY 根据某一字段分组之后,每组显示的结果都只有第一条,这样的结果通常不是我们想要的. GROUP_CONCAT('字段') 可以将每一组下面的这个字段所有的数 ...
- windows----------Windows10 远程桌面连接失败,报CredSSP加密oracle修正错误解决办法
1.通过运行gpedit.msc进入组策略配置(需要win10专业版,家庭版无解),策略路径:“计算机配置”->“管理模板”->“系统”->“凭据分配”,设置名称: 加密 Oracl ...