当前位置:网站首页>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
边栏推荐
- 进程管理基础
- 一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
- Use of fiddler
- 豆瓣平均 9.x,分布式领域的 5 本神书!
- B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
- C#/VB. Net to delete watermarks in word documents
- Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
- FLIR blackfly s usb3 industrial camera: how to use counters and timers
- 所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
猜你喜欢
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
压缩 js 代码就用 terser
#夏日挑战赛#数据库学霸笔记(下)~
This week's hot open source project!
[server data recovery] data recovery case of a Dell server crash caused by raid damage
B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Draco - glTF模型压缩利器
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
豆瓣平均 9.x,分布式领域的 5 本神书!
Web3对法律的需求
随机推荐
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
How to build a 32core raspberry pie cluster from 0 to 1
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
Application analysis of face recognition
Argo workflows source code analysis
C#/VB.NET 删除Word文檔中的水印
Stm32f4 --- PWM output
Summer Challenge database Xueba notes (Part 2)~
[leetcode] day97 remove linked list elements
MetaForce原力元宇宙佛萨奇2.0智能合约系统开发(源码部署)
安德鲁斯—-多媒体编程
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
遇到慢SQL该怎么办?(下)
Real project, realized by wechat applet opening code (end)
企业中台建设新路径——低代码平台
投资的再思考
PostgreSQL图形化界面工具之pgAdmin4