当前位置:网站首页>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
边栏推荐
- 【IoT毕设.上】STM32+机智云AIoT+实验室安全监控系统
- C语言基础知识
- Basis of target detection (NMS)
- 使用 Lambda 函数URL + CloudFront 实现S3镜像回源
- Go integrates logrus to realize log printing
- Leetcode (69) -- square root of X
- Provincial election + noi Part XI others
- When the main process architecture game, to prevent calls everywhere to reduce coupling, how to open the interface to others to call?
- What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
- So programmers make so much money doing private work? It's really delicious
猜你喜欢
玩转MongoDB—搭建MongoDB集群
QT learning management system
Open source internship experience sharing: openeuler software package reinforcement test
sqlilabs less-11~12
sqlilabs less13
被裁三个月,面试到处碰壁,心态已经开始崩了
WebSocket(简单体验版)
Today, with the popularity of micro services, how does service mesh exist?
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
sqlilabs less10
随机推荐
【Flask】Flask启程与实现一个基于Flask的最小应用程序
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
What class loading mechanisms does the JVM have?
Phpcms realizes the direct Alipay payment function of orders
Applet - multiple text line breaks in view
Research Report on development trend and competitive strategy of global vibration polishing machine industry
QT社团管理系统
TDengine 连接器上线 Google Data Studio 应用商店
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
力扣解法汇总241-为运算表达式设计优先级
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
Provincial election + noi Part IX game theory
[NLP] pre training model - gpt1
el-form-item 正则验证
Use lambda function URL + cloudfront to realize S3 image back to source
SWT/ANR问题--当发送ANR/SWT时候如何打开binder trace(BinderTraces)
券商万1免5证券开户是合理安全的吗,怎么讲
[anwangbei 2021] Rev WP
【牛客网刷题系列 之 Verilog快速入门】~ 使用函数实现数据大小端转换
Research Report on the development trend and competitive strategy of the global high temperature label industry