当前位置:网站首页>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
边栏推荐
- postgresql之整體查詢大致過程
- postgresql之integerset
- Stm32f4 --- PWM output
- Dall-E Mini的Mega版本模型发布,已开放下载
- Douban average 9 x. Five God books in the distributed field!
- C#/VB.NET 删除Word文檔中的水印
- 张平安:加快云上数字创新,共建产业智慧生态
- [server data recovery] data recovery case of a Dell server crash caused by raid damage
- Lumion 11.0 software installation package download and installation tutorial
- [leetcode] day97 remove linked list elements
猜你喜欢

张平安:加快云上数字创新,共建产业智慧生态

Overall query process of PostgreSQL

Application analysis of face recognition

The boss is quarantined
![[unity notes] screen coordinates to ugui coordinates](/img/e4/fc18dd9b4b0e36ec3e278e5fb3fd23.jpg)
[unity notes] screen coordinates to ugui coordinates

本周 火火火火 的开源项目!

Lombok makes the pit of ⽤ @data and @builder at the same time

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

New generation cloud native message queue (I)

低代码平台中的数据连接方式(上)
随机推荐
Several classes and functions that must be clarified when using Ceres to slam
FLIR blackfly s usb3 industrial camera: how to use counters and timers
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
AWS学习笔记(一)
PostgreSQL图形化界面工具之pgAdmin4
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
The boss is quarantined
argo workflows源码解析
[C # notes] use file stream to copy files
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
The mega version model of dall-e MINI has been released and is open for download
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
Web3对法律的需求
STM32项目 -- 选题分享(部分)
阿里云易立:云原生如何破解企业降本提效难题?
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Why am I warned that the 'CMAKE_ TOOLCHAIN_ FILE' variable is not used by the project?
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
Introduction to the internal structure of the data directory of PostgreSQL