当前位置:网站首页>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
边栏推荐
- Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
- 玩转gRPC—不同编程语言间通信
- 30 Devops interview questions and answers
- What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
- QT community management system
- C 语言基础
- 【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较
- [IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
- C 语言进阶
- C language ordering management system
猜你喜欢

Using CMD to repair and recover virus infected files

建立自己的网站(21)

In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee

使用 Lambda 函数URL + CloudFront 实现S3镜像回源

Admire, Ali female program undercover more than 500 black production groups

Build your own website (21)

Use of Oracle database objects

使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器

Etcd summary mechanism and usage scenarios

微服务大行其道的今天,Service Mesh是怎样一种存在?
随机推荐
Etcd summary mechanism and usage scenarios
Research Report on development trend and competitive strategy of global consumer glassware industry
Research Report on the development trend and competitive strategy of the global powder filling machine industry
Summary of leetcode's dynamic programming 5
QT community management system
如何看待国企纷纷卸载微软Office改用金山WPS?
Provincial election + noi Part XI others
What class loading mechanisms does the JVM have?
Sorting learning sorting
Halo effect - who says that those with light on their heads are heroes
【NLP】预训练模型——GPT1
QT社团管理系统
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
SWT / anr problem - how to open binder trace (bindertraces) when sending anr / SWT
Realize queue with stack and stack with queue (C language \leetcode\u 232+225)
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
MySQL log
2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
2022 PMP project management examination agile knowledge points (6)