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 ...
随机推荐
- Linux报“ '/usr/bin' is not included in the PATH environment variable”解决方法
https://www.cnblogs.com/alvinwei1024/p/4811993.html https://blog.csdn.net/drbinzhao/article/details/ ...
- 学 shell (1/5)
假设这是某脚本 x.sh 的内容,使用 sh x.sh arg1 来执行该脚本 #!/bin/bashcd `dirname $0`/..source scripts/status.shstart $ ...
- PHP利用ImageMagick把PDF转成PNG
http://blog.csdn.net/longaction2012/article/details/12257867
- Android studio启动后卡在refreshing gradle project
这个问题几乎每个刚使用Android studio的同学都会碰到过,网上有各式各样的方法,有的说使用本地gradle,我试过多次,每次启动Android studio时还是会检查更新,所以根本上解决的 ...
- 【react】---styled-components的基本使用---【巷子】
一.官网地址 https://www.styled-components.com/ 二.styled-components 1.styled-components 样式化组件,主要作用是它可以编写实际 ...
- PHP封装类 【 设置分页 】 !!! 可以直接引用 !!! 都有自己理解的注释,挺详细的,有搜到的朋友可以能帮到你们 【 新手一看练两遍就懂 】
在网页要显示出的内容,就是客户能看到的东西 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- 9月16日,base 福州,2018MAD技术论坛邀您一起探讨最前沿AR技术!
“ 人工智能新一波浪潮带动了语音.AR等技术的快速发展,随着智能手机和智能设备的普及,人机交互的方式也变得越来越自然. 9月16日,由网龙网络公司.msup联合主办的MAD技术论坛将在福州举行.本次论 ...
- SQL Server 2016 共享功能目录 不可修改
x 个人电脑上没有安装MSSQL,以前需要链接数据库写SQL,都是在{VS ->>视图->>SQL Server对象资源管理器}直接链接数据库进行访问操作的... 但是确实有些 ...
- set @sql=N'q',这里的N是什么意思,加与不加有什么区别
用来支持UNICODE字符的,不加的话,如果sql语句中有Unicode字符会认不出来
- [No000014E]提问的智慧How To Ask Questions The Smart Way
原文版本历史 目录 声明 简介 在提问之前 当你提问时 慎选提问的论坛 Stack Overflow 网站和 IRC 论坛 第二步,使用项目邮件列表 使用有意义且描述明确的标题 使问题容易回复 用清晰 ...