LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离。
结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可!
代码如下:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x),left(NULL), right(NULL) {}
};
int minDepth(TreeNode *root)
{
if (NULL == root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right);
if (!l)
return r+;
if (!r)
return l+;
return (l<r)?l+1:r+1;
}
LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy的更多相关文章
- [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 ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [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] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [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] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- Shiro Remember me设置
1. 在Spring的相关配置文件中加入如下Remember me管理器配置: <!-- rememberMe管理器 --> <bean id="rememberMeMan ...
- WMS接口平台配置培训
供应链管理平台地址:https://twms.ninestargroup.com/ibus/#/processconfig?scShortcutld=3_17__1_303 WMS提供WSWMS固定的 ...
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
- Flask最强攻略 - 跟DragonFire学Flask - 第一篇 你好,我叫Flask
首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 一. Python 现阶段三大主流Web框 ...
- 二维树状数组poj1195
题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...
- I/O系统(一)
输入输出系统的发展大致可以分为4个阶段1.早期阶段 特点: 1.1每个IO设备都得有一套独立的逻辑电路和CPU相连. 1.2输入输出过程需要通过CPU,穿插在程序运行的过程中,处理IO时候 ...
- kubernetes 基础
官网 kubernetes.io 有中文 中文网站 http://docs.kubernetes.org.cn kubectl 详细情况 https://kubernetes.io/docs/ref ...
- ipv6 docker
DOCKER, IPV6 BASIC CONFIGURATION OF DOCKER ENGINE WITH IPV6 SEPTEMBER 21, 2015 EYEPV6(AT)GMAIL(DOT)C ...
- HTML5表单_form
原则:能让用户选择的决不填写,增加用户体验 表单(form)元素格式 <input type="text" name="fname" value=&quo ...
- 22. pt-sift
pt-sift /var/lib/pt-stalk/ ======== server01 at 2018_11_23_15_56_46 DEFAULT (1 of 1) ========--disks ...