当前位置:网站首页>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
边栏推荐
- leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
- Application analysis of face recognition
- [C # notes] reading and writing of the contents of text files
- 豆瓣平均 9.x,分布式领域的 5 本神书!
- Detailed explanation of line segment tree (including tested code implementation)
- Summer Challenge database Xueba notes (Part 2)~
- 1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
- 软件测试——Jmeter接口测试之常用断言
- unity webgl自适应网页尺寸
- 进程管理基础
猜你喜欢

How to build a 32core raspberry pie cluster from 0 to 1

Lumion 11.0 software installation package download and installation tutorial
![leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]](/img/0d/e07fe970167368040eb09b05c3682e.png)
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]

Summer Challenge database Xueba notes (Part 2)~

C#/VB.NET 删除Word文档中的水印

云原生混部最后一道防线:节点水位线设计
![[unity notes] screen coordinates to ugui coordinates](/img/e4/fc18dd9b4b0e36ec3e278e5fb3fd23.jpg)
[unity notes] screen coordinates to ugui coordinates

1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l

Application analysis of face recognition

New generation cloud native message queue (I)
随机推荐
go swagger使用
【Node学习笔记】chokidar模块实现文件监听
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
长安链学习笔记-证书研究之证书模式
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
差异与阵列和阵列结构和链表的区别
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
Freeswitch dials extension number source code tracking
Several classes and functions that must be clarified when using Ceres to slam
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
安德鲁斯—-多媒体编程
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
Big guys gather | nextarch foundation cloud development meetup is coming!
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
#夏日挑战赛#数据库学霸笔记(下)~
Douban average 9 x. Five God books in the distributed field!
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】