当前位置:网站首页>Leetcode:minimum_ depth_ of_ binary_ Tree solutions
Leetcode:minimum_ depth_ of_ binary_ Tree solutions
2022-07-07 02:33:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
One 、 The title of
And seek the deepest binary similarity . Given binary tree . Find the minimum depth .
The minimum depth is along the root node , The shortest path to the leaf node .
Two 、 analysis
When I see this topic . I directly changed the code of the deepest binary tree slightly , hold max Change to min. I thought there should be no problem , Who knows WA Twice , I calmed down and looked . Finally I know , When a node is NULL It's about to end . The next time, no matter how simple the topic is, you should calm down and analyze it carefully , Otherwise easy error .
/**
* Definition for binary tree
* 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 0;
int mleft=minDepth(root->left);
int mright=minDepth(root->right);
if(mleft==0)
return 1+mright;
else if(mright==0)
return 1+mleft;
else return min(mleft,mright)+1;
}
};
Two 、
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return minRec(root);
}
int minRec( TreeNode * root) {
if(!root) return 0;
int left = minRec( root->left);
int right = minRec( root->right);
if(left && right) return 1 + min(left, right);
if(left || right) return 1+left+right;
return 1;
}
};
3、 ... and 、
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
return minRec(root);
}
private int minRec(TreeNode root) {
if(root==null) return 0;
int l = minRec(root.left);
int r = minRec(root.right);
if(l==0) return r+1;
if(r==0) return l+1;
return Math.min(l, r) + 1;
}
}
Copyright notice : This article is the original article of the blogger , Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116848.html Link to the original text :https://javaforall.cn
边栏推荐
- Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
- Stm32f4 --- PWM output
- Detailed explanation of line segment tree (including tested code implementation)
- Apifox,你的API接口文档卷成这样了吗?
- Chang'an chain learning notes - certificate model of certificate research
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
- fiddler的使用
- Linear list --- circular linked list
- 差异与阵列和阵列结构和链表的区别
- 投资的再思考
猜你喜欢
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
本周 火火火火 的开源项目!
argo workflows源码解析
低代码平台中的数据连接方式(上)
Big guys gather | nextarch foundation cloud development meetup is coming!
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
How to build a 32core raspberry pie cluster from 0 to 1
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
postgresql之整體查詢大致過程
随机推荐
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
CDB PDB 用户权限管理
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
Yyds dry goods inventory # solve the real problem of famous enterprises: maximum difference
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Argo workflows source code analysis
The empirical asset pricing package (EAP) can be installed through pypi
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
低代码平台中的数据连接方式(上)
Processus général de requête pour PostgreSQL
Lumion 11.0 software installation package download and installation tutorial
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
fiddler的使用
Metaforce force meta universe development and construction - fossage 2.0 system development
Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
Use of fiddler
Rethinking of investment
投资的再思考