当前位置:网站首页>Queue template code
Queue template code
2022-04-23 20:38:00 【Zhongai0049】
For the problem of binary tree , Template code implemented in queue mode
With a sword finger offerII 045. The lowest left value of a binary tree is an example :
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> tree;// Template statement 1
tree.push(root);// Template statement 2
vector<int> row;
while(!tree.empty()){
row.clear();
for(int i = tree.size();i>0;i--){
// Template statement 3
TreeNode* tmp = tree.front();// Template statement 4
row.emplace_back(tmp->val);
if(tmp->left!=nullptr) tree.push(tmp->left);// Template statement 5
if(tmp->right!=nullptr) tree.push(tmp->right);// Template statement 6
tree.pop();// Template statement 7
}
}
return row[0];
}
};
This is mainly aimed at the problem of binary tree , We use sequence traversal to achieve the corresponding subject requirements .
Something to be aware of :
- consider root Empty situation ;
- Consider the case where the queue is empty ;
- The number of each layer of the tree can be used as a queue queue.size() To express , Because every time the root node is out of the queue , Therefore, the elements of a row are always stored in the queue .
版权声明
本文为[Zhongai0049]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210547162952.html
边栏推荐
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- LeetCode 232、用栈实现队列
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- Case of the third day of go language development fresh every day project - news release system II
- UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
- Es index (document name) fuzzy query method (database name fuzzy query method)
- go-zero框架数据库方面避坑指南
- Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- Research on open source OCR engine
猜你喜欢

"Meta function" of tidb 6.0: what is placement rules in SQL?
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window

Click an EL checkbox to select all questions

LeetCode 116. Populate the next right node pointer for each node

DOS command of Intranet penetration

Vscode download speed up

Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
![[graph theory brush question-4] force deduction 778 Swimming in a rising pool](/img/e3/a8cd9fc7773843e9e8ee6a6eba123f.png)
[graph theory brush question-4] force deduction 778 Swimming in a rising pool

Unity solves Z-fighting

Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
随机推荐
Numpy - creation of data type and array
After route link navigation, the sub page does not display the navigation style problem
【PTA】L1-006 连续因子
Case of the third day of go language development fresh every day project - news release system II
Tensorflow 2 basic operation dictionary
SQL gets the latest record of the data table
Leetcode 709, convert to lowercase
Elastic box model
A useless confession artifact
LeetCode 1346、检查整数及其两倍数是否存在
How to protect ECs from hacker attacks?
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
LeetCode 709、转换成小写字母
Rt-1052 learning notes - GPIO architecture analysis
Plato Farm元宇宙IEO上线四大,链上交易颇高
Es index (document name) fuzzy query method (database name fuzzy query method)
Scripy tutorial - (2) write a simple crawler
JSX syntax rules
bounding box iou
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder