LeetCode_Minimum Depth of Binary Tree
一.题目
Minimum Depth of Binary Tree
Total Accepted: 58982 Total Submissions: 202860My
 Submissions
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.
二.解题技巧
因此能够将每一个结点的左右两个子树的深度返回给父节点,父节点选择比較小的深度,然后再返回给祖先结点,以此类推,最后返回给根结点,得到终于结果。
三.实现代码
#include <iostream> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(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)
{
return 0;
} int Result = 1;
int Left = minDepth(root->left);
int Right = minDepth(root->right); if (Left * Right)
{
Result += Left > Right? Right : Left;
}
else
{
Result += Right + Left;
} return Result;
}
};
四.体会


LeetCode_Minimum Depth of Binary Tree的更多相关文章
- [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 ... 
- [LintCode] 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][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 ... 
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
		Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ... 
- 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 111 Minimum Depth of Binary Tree 二叉树
		找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ... 
- Leetcode | Minimum/Maximum Depth of Binary Tree
		Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ... 
随机推荐
- Reverse Words in a String II -- LeetCode
			Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ... 
- bzoj 1305: [CQOI2009]dance跳舞
			题目链接 bzoj 1305: [CQOI2009]dance跳舞 题解 男,女生拆点A1A2,B1B2,拆成两点间分别连容量为K的边,限制与不喜欢的人跳舞的数量 A1连接源点容量为x,B1连接汇点容 ... 
- tomcat重启应用和tomcat重启是两回事。热部署就是重启应用
			tomcat重启应用和tomcat重启是两回事.热部署就是重启应用 tomcat重启应用和tomcat重启是两回事.热部署就是重启应用 tomcat可以设置检测到新的class后重启该应用(不是重启t ... 
- linux 多线程那点事
			说明:对多线程与相互排斥锁不熟悉的请參考其他 #include <pthread.h> #include <stdio.h> #include <stdlib.h> ... 
- kata-container环境搭建
			一.安装qemu 注意,目前kata-container所要求的qemu最低版本是v2.7.0.在笔者的环境下(Ubuntu16.04 VM),apt-get官方源的最高版本是v2.5.0.所以不要用 ... 
- fabricjs 自定义类型
			https://stackoverflow.com/questions/36660108/how-to-create-custom-fabricjs-object I have to create a ... 
- git学习资料及心得
			1. 阮一峰的(简单易懂,实用性佳) http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html http://www.ruanyifeng ... 
- [Apollo Server] Get started with Apollo Server
			Get started with apollo server with node.js: Install: npm install --save apollo-server graphql index ... 
- [PWA] Disable Text Selection and Touch Callouts in a PWA on iOS
			Because an installed PWA is really just a web app running in a browser, there are some browser behav ... 
- 倍福TwinCAT(贝福Beckhoff)基础教程2.2 TwinCAT常见类型使用和转换_字符串
			一般声明字符串都会加一个长度的限制,比如A:STRING(80);至于真实的字符串长度不要超过这个限制即可 在测试中,我演示了两个字符串的方法,CONCAT字符串拼接和REPLACE字符串替换.拼 ... 
