当前位置:网站首页>111. Minimum depth of binary tree
111. Minimum depth of binary tree
2022-04-22 08:40:00 【weixin_ forty-six million two hundred and seventy-two thousand 】
111. Minimum depth of binary tree
Given a binary tree , Find out the maximum depth .
The depth of a binary tree is the number of nodes in the longest path from the root node to the farthest leaf node .
explain : A leaf node is a node that has no children .
Example :
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
Return to its maximum depth 3
Ideas (dfs)
Reference to others , Post link
Source code
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
// If the initial root node is empty , Then return to 0
return 0;
}
if (root->left && root->right) {
// If you change the node, both your sons have , Returns the minimum depth of recursion , Add 1( The height of the node itself is taken into account )
return 1 + min(minDepth(root->left),minDepth(root->right));
}
else if (root->left) {
// If only the left son , His left son , Depth plus 1
return 1 + minDepth(root->left);
}
else if (root->right) {
// If only the left son , Recursive its right son , Depth plus 1
return 1 + minDepth(root->right);
}
else {
// Without a son , Then the depth increases 1( The height of the node itself is taken into account )
return 1;
}
}
};
Ideas ( queue )
Self use pair Construct a data structure , Record the node and path depth respectively , Then construct such a queue .
Because of the nature of queues , Each time, all the steps of the latest step are recorded in , Therefore, the first leaf node that meets the requirements must be the shortest path
Source code
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
queue<pair<TreeNode*,int>> queue;// Construct data structures , The first storage node , Second storage depth
queue.push(pair(root,1));
while (!queue.empty()) {
TreeNode* node = queue.front().first;
int depth = queue.front().second;
queue.pop();
if (node->left == nullptr && node->right == nullptr) {
// The node returns the leaf depth
return depth;
}
if (node->left != nullptr) {
queue.push(pair(node->left,depth+1));// Remember to add one to the depth
}
if (node->right != nullptr) {
queue.push(pair(node->right,depth+1));// Remember to add one to the depth
}
}
return 0;
}
};
版权声明
本文为[weixin_ forty-six million two hundred and seventy-two thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742290986.html
边栏推荐
- 面试题 04.03. 特定深度节点链表(Medium)
- Client and server project 3
- kubernetes—实战入门
- Function pointers and pointer functions
- Arm bare metal (IV) -- exceptions and interrupts
- JSON数据文本
- Seven crimes of hackers in social engineering -- hooking
- 第1关:创建/删除节点
- 100. 相同的树(Easy)
- Under the new retail development trend, how to operate and promote the social e-commerce platform?
猜你喜欢

94. Middle order traversal of binary tree (easy)

第1关:节点监听机制

129. 求根节点到叶节点数字之和(Medium)

CentOS 安装 MySQL

Algorithm -- delete the penultimate node of the linked list (kotlin)

OLED display driver

CentOS MySQL installation

Nacos Foundation (4): configure the external database of Nacos

Nacos Foundation (1): what is configuration center & introduction to Nacos

The domestic cloud security market has exceeded 10 billion yuan. What is the future development trend?
随机推荐
第1关:创建/删除节点
Handler related source code analysis
cesium中实现楼层分解动画
Cesium加载地形数据(cesiumlab制作地形数据),从源数据到地形服务
指针和字符串
Winsock编程接口实验:实现ipconfig
第3关:节点状态检查、数据查看和更新
Fundamentals of go language (1)
Level 1: node monitoring mechanism
pictures rotating
客户端与服务器通信项目4
Common sense and use of Oracle Database
Monkey eating peach problem (loop, recursion)
面试题 04.03. 特定深度节点链表(Medium)
MaterialApp
Nacos Foundation (1): what is configuration center & introduction to Nacos
redis 简单使用
Level 2: create, list and delete child nodes
Mapbox设置官方地图语言为中文
JS judge the element to the top and fix it