LeetCode111. Minimum Depth of Binary Tree
题目
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树
[3,9,20,null,null,15,7],3
/ \
9 20
/ \
15 7返回它的最小深度 2.
考点
dfs.
递归
思路
代码
1.递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
//root==nullptr,0
if(!root)
return 0;
//left==nullptr->f(right)+1
if(!root->left) return 1+minDepth(root->right);
//right==nullptr->f(left)+1
if(!root->right) return 1+minDepth(root->left);
//none of above==nullptr->min(f(left),f(right))+1
return 1+min(minDepth(root->left),minDepth(root->right));
}
};
问题
LeetCode111. Minimum Depth of Binary Tree的更多相关文章
- [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 ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- 【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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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 (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- C++中遇到的各种小问题
lpcwstr类型问题 在使用VS2010开发C++程序时,由于系统默认字符集是unicode字符集,造成与早期的字符串格式不兼容问题 ①Properties — Configuration Prop ...
- [转]JS对JSON的操作总结
本文转自:http://www.cnblogs.com/csj222/archive/2013/04/11/3013667.html 对于前端完全是菜鸟,迫于无奈,工作中要用到JS,尤其对JSON的处 ...
- usually study notebook
2018/01/02 删除用户经验: 1,vi /etc/passwd ,然后注释掉用户,观察一个月,以便于还原,相当于备份. 2,把登入shell改成/sbin/nologin. 3,openlda ...
- rail 怎样在已有数据库上继续开发
今天刷贴看到了这篇文章http://ruby-china.org/topics/16493,老大回复的很有意义,在这里备份一个 而要把现有的数据库纳入 Migration,一个简单方法: 创建一个空 ...
- pat1086. Tree Traversals Again (25)
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Windows 编程
在WndProc函数中 最好不要出现WM_SYSCOMMAND消息, 如果有了这个消息, 可能我们对创建出来的窗口就什么都管不了了, 因为我们阻碍了DefWndProc函数去处理它 不在.rc文件中添 ...
- GitKraken使用教程-基础部分(3)
5. 克隆服务器上的项目 首先,返回主界面,点击File => Clone Repo,选择 Clone with URL,如下图: 图 5‑1 SSH方式克隆仓库界面 1) SSH 方式连接仓库 ...
- 文件夹选择之FolderBrowserDialog控件
应用程序可能只允许用户选择文佳夹而非文件,例如在播放MP3时,用户可能把所有的MP3放在一个文佳夹内,在添加时,只要选择添加这个文佳夹,将会把在这个文件内的所有MP3添加的播放器里.在这里对播放器来说 ...
- Design Pattern ->Composite
Layering & Contract Philosophy With additional indirection class CComponent { ; ; ; public: virt ...
- XCode 如何真机运行别人的demo项目
iOS应用安装到真机需要证书和mobileprovision 文件,拿到别人的项目 是没有这些的 ,也就运行不起来. 要想运行起来, 需要选中项目, target - > 修改 bundlei ...