当前位置:网站首页>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
边栏推荐
- 真实项目,用微信小程序开门编码实现(完结)
- STM32 project -- Topic sharing (part)
- Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- [xlua notes] array of lua to array of C #
- ODBC database connection of MFC windows programming [147] (with source code)
- Processus général de requête pour PostgreSQL
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- 最近小程序开发记录
猜你喜欢

1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效

String or binary data will be truncated

【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例

处理streamlit库上传的图片文件

Data connection mode in low code platform (Part 1)

C#/VB.NET 删除Word文档中的水印

3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP

Use of fiddler

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

老板被隔离了
随机推荐
[C # notes] use file stream to copy files
FLIR blackfly s usb3 industrial camera: how to use counters and timers
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
老板被隔离了
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
【LeetCode】Day97-移除链表元素
[leetcode] day97 remove linked list elements
Data connection mode in low code platform (Part 1)
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Compress JS code with terser
企业中台建设新路径——低代码平台
String or binary data will be truncated
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Web3的先锋兵:虚拟人
Web开发小妙招:巧用ThreadLocal规避层层传值
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
Robot team learning method to achieve 8.8 times human return
Use of fiddler
C语言练习题_1