当前位置:网站首页>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
边栏推荐
- [Mori city] random talk on GIS data (II)
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- Rethinking of investment
- 4 -- Xintang nuc980 mount initramfs NFS file system
- #yyds干货盘点# 解决名企真题:最大差值
- Compress JS code with terser
- Processus général de requête pour PostgreSQL
- Use of pgpool II and pgpooladmin
- [unity notes] screen coordinates to ugui coordinates
- Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
猜你喜欢
Data connection mode in low code platform (Part 1)
How can reinforcement learning be used in medical imaging? A review of Emory University's latest "reinforcement learning medical image analysis", which expounds the latest RL medical image analysis co
B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
Detailed explanation of line segment tree (including tested code implementation)
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
Web3对法律的需求
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
Infrared camera: juge infrared mag32 product introduction
压缩 js 代码就用 terser
你不可不知道的Selenium 8种元素定位方法,简单且实用
随机推荐
Compress JS code with terser
Here comes a white paper to uncover the technology behind Clickhouse, a node with 10000 bytes!
所谓的消费互联网仅仅只是做行业信息的撮合和对接,并不改变产业本身
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Lumion 11.0软件安装包下载及安装教程
6-6漏洞利用-SSH安全防御
go swagger使用
差异与阵列和阵列结构和链表的区别
Argo workflows source code analysis
pgpool-II和pgpoolAdmin的使用
Untiy文本框的代码换行问题
Sensor: DS1302 clock chip and driver code
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
老板被隔离了
Difference and the difference between array and array structure and linked list
投资的再思考
4--新唐nuc980 挂载initramfs nfs文件系统
Use of pgpool II and pgpooladmin
Application analysis of face recognition