【leetcode】Minimum Depth of Binary Tree (easy)
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.
简单题,不过注意叶子结点是两个子树都是NULL的指针。只有一个子树NULL的不是。
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)
{
return ;
}
if((root->left == NULL)&&(root->right == NULL))
{
return ;
}
else if(root->left == NULL)
{
return minDepth(root->right) + ;
}
else if(root->right == NULL)
{
return minDepth(root->left) + ;
}
else
{
return min(minDepth(root->left), minDepth(root->right)) + ;
}
}
};
【leetcode】Minimum Depth of Binary Tree (easy)的更多相关文章
- 【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】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】Maximum Depth of Binary Tree
http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...
- 【LeetCode练习题】Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- 如何让 height:100%; 起作用
当你设置一个页面元素的高度(height)为100%时,期望这样元素能撑满整个浏览器窗口的高度,但大多数情况下,这样的做法没有任何效果.你知道为什么height:100%不起作用吗? 按常理,当我们用 ...
- [译]git remote
git remote命令让我们可以创建, 查看, 删除一个到其他仓储的连结. 下图展示了我们的本地仓储有两个remote连接, 一个是中央仓储, 一个是其他开发者的仓储. 除了使用完整的url指向他们 ...
- jquery选择器(一)-基础选择器
1. ID元素选择器 $("#btn1") 2. class元素选择器 $(".btn") 3. 标签元素选择器 $("div") 4. 全 ...
- C#入门随手笔记
1..Net Framework是Microsoft为开发应用程序而创建的一个开发平台. 运行操作系统不限:Microsoft版本运行在windows,Mono版本运行开Linux和MacOS: 应用 ...
- MyEclipse 2013优化配置【转】
作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs MyEclipse 2013优化速度方案仍然主要有这么几个方面:去除无需加载的模块.取消冗余的配置.去除不 ...
- android定位
先说说手机定位的方式 1,GPS 绝大部分手机都有GPS模块,这种方式准确度是最高的,但是缺点也很明显,1,耗电高:2,绝大部分用户默认不开启GPS模块.3,从GPS模块启动到获取第一次定位数据,可能 ...
- spring 控制事务
<!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org. ...
- Attempt to present <vc> on <vc> which is already presenting <vc>/(null)
在给 tableViewCell 添加长按手势弹出一个 popViewController 的时候,遇到的这个变态问题: Warning: Attempt to present <UINavig ...
- jtable插件api
官网2016-03-15 事例图: 一.客户端配置 1. paging boolean default:false 配置是否分页,果断改为true. 2. pageList string defaul ...
- C语言中#define的用法(转)
转自:http://www.dingge.com/main/article.asp?id=10 今天整理了一些#define的用法,与大家共享! 1.简单的define定义 #define MAXTI ...