当前位置:网站首页>Leetcode:minimum_depth_of_binary_tree解决问题的方法
Leetcode:minimum_depth_of_binary_tree解决问题的方法
2022-07-06 18:47:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
一、 称号
并寻求最深的二元相似。给定的二进制树。求其最小深度。
最小深度是沿从根节点,到叶节点最短的路径。
二、 分析
当我看到这个题目时。我直接将最深二叉树的代码略微改了下,把max改成min。本以为应该没有问题,谁知道WA了两次,我静下来看了看。最终知道了,当遇到有结点为NULL时就得要结束了。所下面次再简单的题目也要静下来好好分析,不然会easy出错。
/**
* 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;
}
};
二、
* 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;
}
};
三、
/**
* 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;
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116848.html原文链接:https://javaforall.cn
边栏推荐
- 【森城市】GIS数据漫谈(二)
- Apifox,你的API接口文档卷成这样了吗?
- C语言练习题_1
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- 【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
- The boss is quarantined
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- #yyds干货盘点# 解决名企真题:最大差值
- A new path for enterprise mid Platform Construction -- low code platform
猜你喜欢
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
Stm32f4 --- PWM output
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
C # / vb. Net supprime le filigrane d'un document word
FLIR blackfly s usb3 industrial camera: how to use counters and timers
Time synchronization of livox lidar hardware -- PPS method
Blackfly s usb3 industrial camera: buffer processing
String or binary data will be truncated
The last line of defense of cloud primary mixing department: node waterline design
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
随机推荐
What to do when encountering slow SQL? (next)
【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
最近小程序开发记录
Chang'an chain learning notes - certificate model of certificate research
Big guys gather | nextarch foundation cloud development meetup is coming!
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
C#/VB.NET 删除Word文檔中的水印
postgresql之integerset
老板被隔离了
Apifox,你的API接口文档卷成这样了吗?
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!
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
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
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
[paper reading | deep reading] graphsage:inductive representation learning on large graphs
处理streamlit库上传的图片文件
leetcode:736. Lisp 语法解析【花里胡哨 + 栈 + 状态enumaotu + slots】