LeetCode OJ: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.
求二叉树的最小深度,额,简单的递归而已,代码如下:
class Solution {
public:
int minDepth(TreeNode* root) {
if(root = NULL) return ;
int leftDep = minDepth(root->left);
int rightDep = minDepth(root->right);
if(leftDep == && rightDep == ) return ;//叶子节点
if(leftDep == ) leftDep = INT_MAX;
if(rightDep == ) rightDep = INT_MAX;//中间节点
return min(leftDep, rightDep) + ;
}
};
java 版本的代码一样,与上面相同,代码如下所示:
/**
* 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;
int leftDep = minDepth(root.left);
int rightDep = minDepth(root.right);
if(leftDep == 0 && rightDep == 0)
return 1; //到达这里表明的是叶子节点
if(leftDep == 0) leftDep = Integer.MAX_VALUE;
if(rightDep == 0) rightDep = Integer.MAX_VALUE;
return Math.min(leftDep, rightDep) + 1;
}
}
LeetCode OJ: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] 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 ...
- [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 | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- 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 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode OJ——Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...
随机推荐
- 瑞丽熵(renyi entropy)
在信息论中,Rényi熵是Hartley熵,Shannon熵,碰撞熵和最小熵的推广.熵能量化了系统的多样性,不确定性或随机性.Rényi熵以AlfrédRényi命名.在分形维数估计的背景下,Rény ...
- springboot 项目跨域问题 CORS
package com.example.demo.cors; import org.springframework.context.annotation.Bean; import org.spring ...
- Facebook支持python的开源预测工具Prophet
Facebook 宣布开源一款基于 Python 和 R 语言的数据预测工具――“Prophet”,即“先知”.取名倒是非常直白. Facebook 表示,Prophet 相比现有预测工具更加人性化, ...
- SMARTFORM 传值的4种方法
*& 20161019 160300 smartform传值的方法 1.通过结构 传值: 最通常的用法是通过SE11中建立STRUCTURE XXX(表则没用),在REPORT中申明此结构的数 ...
- 2.6 基于ARDUINO UNO+MC20的路径显示功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- JavaScript Date to c# Ticks
var ticks =((yourDateObject.getTime()*10000)+621355968000000000); var minDate = new Date("2013& ...
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- linux eclipse的桌面快捷方式
在桌面上创建一个eclipse.desktop [Desktop Entry] Encoding=UTF- Name=Eclipse Comment=Eclipse IDE Exec=/opt/Dev ...
- 025_MapReduce样例Hadoop TopKey算法
1.需求说明