当前位置:网站首页>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
边栏推荐
- 最近小程序开发记录
- Big guys gather | nextarch foundation cloud development meetup is coming!
- The cities research center of New York University recruits master of science and postdoctoral students
- Freeswitch dials extension number source code tracking
- 纽约大学 CITIES 研究中心招聘理学硕士和博士后
- 强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
- Processus général de requête pour PostgreSQL
- unity webgl自适应网页尺寸
- Use of fiddler
- C#/VB.NET 删除Word文檔中的水印
猜你喜欢

Word wrap when flex exceeds width

Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code

fiddler的使用

豆瓣平均 9.x,分布式领域的 5 本神书!

Time synchronization of livox lidar hardware -- PPS method

Draco - glTF模型压缩利器

Stm32f4 --- PWM output

【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks

SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!

B站6月榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
随机推荐
[unity notes] screen coordinates to ugui coordinates
Robot team learning method to achieve 8.8 times human return
GEE升级,可以实现一件run tasks
The cities research center of New York University recruits master of science and postdoctoral students
6 seconds to understand the book to the Kindle
The boss is quarantined
Argo workflows source code analysis
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
Freeswitch dials extension number source code tracking
This week's hot open source project!
MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
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
FLIR blackfly s industrial camera: auto exposure configuration and code
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Lombok makes the pit of ⽤ @data and @builder at the same time
Lumion 11.0 software installation package download and installation tutorial
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
pgpool-II和pgpoolAdmin的使用
【LeetCode】Day97-移除链表元素