当前位置:网站首页>111. Minimum depth of binary tree
111. Minimum depth of binary tree
2022-07-01 14:21:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
subject :
Given a binary tree , Find out the minimum depth .
The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node .
explain : A leaf node is a node that has no children .
Example 1:

| Input :root = [3,9,20,null,null,15,7] Output :2 |
Example 2:
| Input :root = [2,null,3,null,4,null,5,null,6] Output :5 |
Tips :
| The number of nodes in the tree ranges from [0, 105] Inside -1000 <= Node.val <= 1000 |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/minimum-depth-of-binary-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
For topic description, concept description :

Minimum depth of leaf node = 1
The minimum depth of the parent node depends on its child nodes , Take the minimum value of its minimum depth , add 1
such as :
node 4, The left child node = 1, The right child node = 1, So node 4 = 1+1
node 2, Only the left child nodes , Then the node 2 = 2+1
The whole diagram shows that the minimum depth of the root node is the minimum depth of traversing its child nodes
Typical recursion
Method 1 、 recursive
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int minDepth(struct TreeNode* root){
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
int min = INT_MAX;
if(root->left)
{
min = fmin(min, minDepth(root->left));
}
if(root->right)
{
min = fmin(min, minDepth(root->right));
}
return min + 1;
}One thing to note :
At the beginning, the commonly used Macro definition minimum
As a result, the last group of big data timed out
// cause timeout
#define mymin(a,b) ( (a) < (b) ? (a) : (b) )
And the reason is that :
The macro definition is just a simple replacement , If the parameter is a simple number , Actually, no problem
But if the parameter is something that needs to be calculated , Recursive functions are also used here
Then it is calculated once when it is passed in , Calculate again when returning , This magnitude is super large
边栏推荐
- 玩转gRPC—不同编程语言间通信
- Etcd summary mechanism and usage scenarios
- 8 best practices to protect your IAC security!
- WebSocket(简单体验版)
- 数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
- 建立自己的网站(21)
- 当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
- 力扣解法汇总241-为运算表达式设计优先级
- 百度上找的期货公司安全吗?期货公司怎么确定正规
- Fiori applications are shared through the enhancement of adaptation project
猜你喜欢

博文推荐 | 深入研究 Pulsar 中的消息分块

So programmers make so much money doing private work? It's really delicious

TexStudio使用教程

算网融合赋能行业转型,移动云点亮数智未来新路标

Texstudio tutorial

This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words

开源实习经验分享:openEuler软件包加固测试

App自动化测试开元平台Appium-runner

Using CMD to repair and recover virus infected files
随机推荐
App自动化测试开元平台Appium-runner
自定义注解实现验证信息的功能
Research Report on development trend and competitive strategy of global vibration polishing machine industry
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
MySQL日志
日志中打印统计信息的方案
El form item regular verification
2022 PMP project management examination agile knowledge points (6)
Research Report on the development trend and competitive strategy of the global high temperature label industry
如何看待国企纷纷卸载微软Office改用金山WPS?
Research Report on the development trend and competitive strategy of the global axis measurement system industry
百度上找的期货公司安全吗?期货公司怎么确定正规
使用CMD修复和恢复病毒感染文件
TexStudio使用教程
WebSocket(简单体验版)
sqlilabs less10
2022-2-15 learning the imitation Niuke project - post in Section 2
2022. Let me take you from getting started to mastering jetpack architecture components - lifecycle
Open source internship experience sharing: openeuler software package reinforcement test