当前位置:网站首页>The 15th day of the special assault version of the sword offer
The 15th day of the special assault version of the sword offer
2022-07-31 04:45:00 【hys__handsome】
简单的层序遍历
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if(!root) return {
};
queue<TreeNode*> que;
que.push(root);
vector<int> res;
while(que.size()) {
int sz = que.size();
int maxx = INT_MIN;
for(int i = 0; i < sz; i++) {
auto cur = que.front(); que.pop();
maxx = max(maxx,cur->val);
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
res.push_back(maxx);
}
return res;
}
};
层序遍历,Each time a layer sequence is recorded, if there is a next layer, the current sequence will be cleared and the next layer sequence will be recorded,Finally, the first one returned is the bottom leftmost node
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
que.push(root);
vector<int> backup;
while(que.size()){
int sz = que.size();
backup.clear();
for(int i = 0; i < sz; i++) {
auto cur = que.front(); que.pop();
backup.push_back(cur->val);
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return backup[0];
}
};
剑指 Offer II 046. 二叉树的右侧视图
The layer order traversal records the last one of each layer
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
if(!root) return {
};
que<TreeNode*> que;
que.push(root);
vector<int> res;
while(que.size()) {
int last = -1, sz = que.size();
for(int i = 0; i < sz; i++) {
auto cur = que.front(); que.pop();
last = cur->val;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
res.push_back(last);
}
return res;
}
};
边栏推荐
- three.js make 3D photo album
- 聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
- [shell basics] determine whether the directory is empty
- MySQL database backup
- MySQL database must add, delete, search and modify operations (CRUD)
- Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
- Heavyweight | The Open Atomic School Source Line activity was officially launched
- idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
- Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
- HCIP第十天_BGP路由汇总实验
猜你喜欢

Unity框架设计系列:Unity 如何设计网络框架

Exsl file preview, word file preview web page method

XSS靶场(三)prompt to win

Heavyweight | The Open Atomic School Source Line activity was officially launched

MySQL database backup

强化学习:从入门到入坑再到拉屎

XSS shooting range (3) prompt to win

binom二项分布,

【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用

idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
随机推荐
Unity URP渲染管线摄像机核心机制剖析
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
[C language] Detailed explanation of operators
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
Learning DAVID Database (1)
three.js make 3D photo album
关于出现大量close_wait状态的理解
Can't load /home/Iot/.rnd into RNG
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
扫雷小游戏——C语言
visual studio 那些提高效率的快捷键,总结(不时更新)
Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
Recursive implementation of the Tower of Hanoi problem
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Unity教程:URP渲染管线实战教程系列【1】
问题7:列表的拼接
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
ClickHouse: Setting up remote connections