当前位置:网站首页>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
边栏推荐
- 如何设计好接口测试用例?教你几个小技巧,轻松稿定
- C#/VB.NET 删除Word文档中的水印
- 1个月增长900w+播放!总结B站顶流恰饭的2个新趋势
- PostgreSQL图形化界面工具之pgAdmin4
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- 企业中台建设新路径——低代码平台
- Apifox,你的API接口文档卷成这样了吗?
- Linear list --- circular linked list
- A new path for enterprise mid Platform Construction -- low code platform
- 低代码平台中的数据连接方式(上)
猜你喜欢
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Introduction to FLIR blackfly s industrial camera
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Go swagger use
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
6-6漏洞利用-SSH安全防御
Lumion 11.0软件安装包下载及安装教程
C # / vb. Net supprime le filigrane d'un document word
3D laser slam: time synchronization of livox lidar hardware
Overall query process of PostgreSQL
随机推荐
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
Linear list --- circular linked list
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
#yyds干货盘点# 解决名企真题:最大差值
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
Halcon instance to opencvsharp (C openCV) implementation -- bottle mouth defect detection (with source code)
Web3对法律的需求
老板被隔离了
安全巡检的工作
Overall query process of PostgreSQL
CDB PDB 用户权限管理
Big guys gather | nextarch foundation cloud development meetup is coming!
Introduction to the internal structure of the data directory of PostgreSQL
How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
Compress JS code with terser
如何设计好接口测试用例?教你几个小技巧,轻松稿定
The boss is quarantined