当前位置:网站首页>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
边栏推荐
- Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
- Dall-E Mini的Mega版本模型发布,已开放下载
- The last line of defense of cloud primary mixing department: node waterline design
- 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
- [C # notes] use file stream to copy files
- 【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity
- 企业中台建设新路径——低代码平台
- B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
- 投资的再思考
- AWS学习笔记(一)
猜你喜欢
Lumion 11.0软件安装包下载及安装教程
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
云原生混部最后一道防线:节点水位线设计
Summer Challenge database Xueba notes (Part 2)~
Introduction to FLIR blackfly s industrial camera
Processus général de requête pour PostgreSQL
postgresql之integerset
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
postgresql之整體查詢大致過程
压缩 js 代码就用 terser
随机推荐
unity 自定义webgl打包模板
FLIR blackfly s industrial camera: auto exposure configuration and code
Untiy文本框的代码换行问题
MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks
leetcode:5. Longest palindrome substring [DP + holding the tail of timeout]
压缩 js 代码就用 terser
企业中台建设新路径——低代码平台
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
Big guys gather | nextarch foundation cloud development meetup is coming!
Draco - gltf model compression tool
Pioneer of Web3: virtual human
Pgadmin4 of PostgreSQL graphical interface tool
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity
Chang'an chain learning notes - certificate model of certificate research
C#/VB. Net to delete watermarks in word documents
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
Douban average 9 x. Five God books in the distributed field!
Metaforce force meta universe development and construction - fossage 2.0 system development