LeetCode-111.Mininum 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.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its minimum depth = 2.
public int minDepth(TreeNode root) {//树 递归 mytip
if(null==root){
return 0;
}
int left = minDepth(root.left);
int right = minDepth(root.right);
int d=left>right?(right+1):(left+1);
if(left==0||right==0){//注意 单孩子的判断
d= left+right+1;
}
return d;
}
还可以使用BFS的方法
相关题
二叉树的最大深度 LeetCode104 https://www.cnblogs.com/zhacai/p/10429258.html
LeetCode-111.Mininum Depth of Binary Tree的更多相关文章
- [leetcode] 111.Mininum Depth of Binary Tree (Easy)
原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...
- [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 ...
- 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 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
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- (二叉树 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 ...
- 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 ...
随机推荐
- Spring Data Jpa 使用Left Join
准备: Spring Boot + Web + Jpa 代码: 类:AccountRepository @Query(value = "select new com.sino.report. ...
- c# 根据字段名,得到对象中的属性值
public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj.GetType(); object o ...
- spring boot mybatis 整合教程
本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: 1.5.6 jdk:1.8.0_161 maven:3.3.9 额外功能 PageHelper 分页 ...
- Javascript Base64加密解密代码
<script language="javascript" runat="server"> var keyStr = "ABCDEFGHI ...
- Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪?(已解决)
Linux(Ubuntu)使用 sudo apt-get install 命令安装软件的目录在哪? bin文件路径: /usr/bin 库文件路径: /usr/lib/ 其它的图标啊什么的路径 ...
- E - TOYS
来源 poj 2318 Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad ...
- DOM内容操作
<table border="2"> <thead id="1" class="c1 c2"> <tr> ...
- python 中 打印及格式化字符串的相关方法
原文 将值转换为字符串 Python 有多种方式将任何值转为字符串: 将它传给 repr() 或 str() 函数. repr() 和 str() 的区别,看几个例子: >>> pr ...
- easyui---editgrid
on 点击新增用户,不是弹出一个一个dialog,而是直接在表格下面增加一行可编辑的,然后点击保存就可以新增 第一步:加一个toolbar,在handler中当点击新增用户,会调用datagrid的a ...
- debian安装nodejs
https://nodejs.org/en/download/ 二进制安装 sudo wget https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux ...