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 ...
随机推荐
- android intent打开各种文件的方法
android intent打开各种文件的方法 1./** * 检测是否安装了某个软件 * * @param pkgName "com.bill99.kuaishua" ...
- ffmpeg yasm not found, use --disable-yasm for a crippled build
yasm是汇编编译器,因为ffmpeg中为了提高效率用到了汇编指令,比如MMX和SSE.解决这个问题方面有两个: 1.在网上下载一个yasm.exe并安装在mingw/bin下面,编译代码时你注意看, ...
- Ubuntu 16.04桌面版GUI网络配置工具NetworkManager的命令行工具nm-tool无法使用的问题
说明: 1.Ubuntu中分桌面版和服务器版,而这两个版本在网络管理方面使用的工具都不一样,尤其是在桌面版,使用了NetworkManager进行管理. 2.服务器版使用的是命令行配置,而桌面版包含了 ...
- uprobes issue with oracle 12c
https://mahmoudhatem.wordpress.com/2017/03/21/uprobes-issue-with-oracle-12c/
- Gradle Distributions
Gradle Distributions services.gradle.org/ distributions/ gradle-3.4-rc-3-all.zip 13-Feb-2017 14:55 + ...
- JS方面重点摘要(二)
1.函数声明与函数表达式 (1)变量声明会置顶提前,但赋值仍在原地方(2)函数声明同变量声明一样会提前:但是,函数表达式没有提前,就相当于平时的变量赋值(3)函数声明会覆盖变量声明,但不会覆盖变量赋值 ...
- MySQL的id生成策略
1 自增 CREATE TABLE `test` ( `id` ) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAUL ...
- 使navicat可以通过SSH连接MySQL数据库
1.编辑/etc/ssh/sshd_config,在最下面添加如下语句 KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libss ...
- 通过项目了解JAVA注解
java自定义注解实践 ² 背景 最近在为公司的技术改造做准备,我设计了一个提高Web开发效率的技术框架,为了增加框架的友好性和易用性,决定采用注解来代替配置文件,于是我查询了很多的资料,进行整理和学 ...
- Roboware 下打包成so 文件并引用
一.生成.so文件 在ros中编译.so文件,如同在vs中编译C++版的dll文件.具体步骤如下: 步骤1: 首先建立.h文件和一个.cpp文件(该.cpp文件就是此次封装的内容) 步骤2: ...