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 ...
随机推荐
- Mac开发环境配置 就喜欢折腾
暑假放假,开始用自己的电脑配置开发环境进行学习. 虚拟机安装VMware tools也遇到一些问题.折腾之后解决,遇到各种问题,谷歌后都能解决. 折腾了几天虚拟机下的Ubuntu,发现mac带虚拟机太 ...
- JavaScript实现自适应窗口大小的网页
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- Meta 数据中文显示
class Meta:verbose_name='待办事项'verbose_name_plural = verbose_name这是什么意思? verbose_name指定在admin管理界面中显 ...
- MSDN使用
比如我想查一下fopen这个函数怎么用,在索引里搜索一下fopen,很容易找到了. 但是如果我想横向扩展一下,查看一些与fopen相关的函数,应该怎么找呢? 很简单,点击定位: 你就能把fopen定位 ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【转载】格式化存储装置成为 Ext2/Ext3/Ext4 档案系统
格式化 用系统管理员帐户 (即 root) 身份打「mkfs -t ext2|ext3|ext4 储存装置」: mkfs -t ext3 /dev/sdb5 要格式化档案系统为 Ext2,亦可以直接使 ...
- MongoDB的Find详解(一)
1.指定返回的键 db.[documentName].find ({条件},{键指定}) 数据准备persons.json var persons = [{name:"jim",a ...
- vue项目的webpack设置请求模拟数据的接口方法
最近在跟着视频写饿了吗vue项目,其中模拟数据由于webpack版本变化,跟视频中不一致,下方博客有解决方案,其实视频里面的还能看懂,现在webpack的服务都在插件包里了,好难找. 请参考:http ...
- CSS3手风琴菜单 可同时折叠多个菜单
在线演示 本地下载
- poj 2828 Buy Tickets 【买票插队找位置 输出最后的位置序列+线段树】
题目地址:http://poj.org/problem?id=2828 Sample Input 4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31 ...