树——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.
解决思路:
1. 当节点为叶节点时,深度为1;
2. 当节点只有左孩子(或右孩子)时,深度为左孩子(或右孩子)+1;
3. 当节点既有左孩子又有右孩子时, 深度为min(左孩子,右孩子)+1。
代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int run(TreeNode root) {
if(root==null)
return 0;
if(root.left==null && root.right==null)
return 1;
if(root.left==null && root.right!=null)
return run(root.right)+1;
if(root.left!=null && root.right==null)
return run(root.left)+1;
return Math.min(run(root.left), run(root.right))+1;
}
}
树——minimum-depth-of-binary-tree(二叉树的最小深度)的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 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] The minimum depth of binary tree二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- [LeetCode] 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 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- N1-1 - 树 - Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...
随机推荐
- es之分词器和分析器
Elasticsearch这种全文搜索引擎,会用某种算法对建立的文档进行分析,从文档中提取出有效信息(Token) 对于es来说,有内置的分析器(Analyzer)和分词器(Tokenizer) 1: ...
- [CSP-S模拟测试]:礼物(数学)
题目传送门(内部题80) 输入格式 第一行输入一个正整数$n$. 第二行到第$n+1$行每行两个正整数$a_i$和$b_i$表示第$i$个礼物中包含$a_i$个红宝石和$b_i$个绿宝石. 输出格式 ...
- Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...
- ListView 九宫格布局实现
1.效果图 2.数据 SettingData.json { "data": [{ "icon":"setting", "title ...
- Vue知识整理2:Vue生命周期方法
在vue执行过程中,可以分为beforeCreate.created.BeforeMount.mounted .BeforeUpdate.updated 等常用的方法,如下图所示. 除此之外,通过查 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_14_静态static的内存图
输出room的时候,推荐用类名称点的形式 方法区内有,有个独立的空间叫做静态区,专门用来存储静态static的数据 下图红色箭头的部分,全程和对象没有关系.
- Zotero引用文献格式(软件学报)
最近在写一篇综述,要处理大量引用文献,选用Zotero作为文献管理工具.在插入参考文献目录时需要遵循格式,奈何网上找不到<软件学报>对应的csl模板文件,所以决定自己动手修改.在此记录下自 ...
- python笔记01(详情请看廖雪峰的官方网站)
python 在调用函数的时候, 如果传入的参数数量不对, 如果传入的参数类型不对 会报TypeError的错误,并且Python会明确提示参数错误原因. hex()内置函数会把一个整数转换成十六进制 ...
- Ubuntu操作系统的总结操作
一.Ubuntu系统环境变量 Ubuntu Linux系统环境变量配置文件分为两种:系统级文件和用户级文件 1.系统级文件: /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件 ...
- MD5加密 和 自定义加密解密
public class EncryptString { /// <summary> /// MD5加密 /// </summary> /// <param name=& ...