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 ...
随机推荐
- luogu P1146 硬币翻转
题目描述 在桌面上有一排硬币,共N枚,每一枚硬币均为正面朝上.现在要把所有的硬币翻转成反面朝上,规则是每次可翻转任意N-1枚硬币(正面向上的被翻转为反面向上,反之亦然).求一个最短的操作序列(将每次翻 ...
- php的function() use($args)用法
使用use返回 aaa aaa.使用函数传参数aaa bbb. use的参数必须是已经存在的,如果没有定义返回Notice: Undefined variable: word ,使用函数参数方式不需要 ...
- extjs combo中给Store插入一条数据
{ xtype: 'combo', columnWidth: .55, name: 'AQLLevel', store: Ext.create('Scripts.Code.Common.store.I ...
- 如何命令行编译Java工程
在src下的包含Main的包下打开命令行,javac -classpath “路径到src,不到包下” Main.java
- js中typeof的用法汇总
- UVa 1218 - Perfect Service
/*---UVa 1218 - Perfect Service ---首先对状态进行划分: ---dp[u][0]:u是服务器,则u的子节点可以是也可以不是服务器 ---dp[u][1]:u不是服务器 ...
- 区别dependencies、devDependencies
我们在使用npm install 安装模块或插件的时候,有两种命令把他们写入到 package.json 文件里面去,他们是: --save-dev 或 --save 首先需要说明的是Dependen ...
- ylb: 数据库操作方法基础
ylbtech-SQL Server:SQL Server-数据库操作方法基础 数据库操作方法基础. ylb: 数据库操作方法基础 返回顶部 ----------试图操作(view)--------- ...
- 15个最受欢迎的Python开源框架(转载)
一.Django: Python Web应用开发框架 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的软件设计模式,即模型M,视图V和控制器C.它最初是被开发来用于管理 ...
- etcd的原理分析
k8s集群使用etcd作为它的数据后端,etcd是一种无状态的分布式数据存储集群. 数据以key-value的形式存储在其中. 今天同事针对etcd集群的运作原理做了一个讲座,总结一下. A. etc ...