当前位置:网站首页>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;
}
};
边栏推荐
- Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
- View source and switch mirrors in two ways: npm and nrm
- Basic knowledge of mysql (2)
- 微软 AI 量化投资平台 Qlib 体验
- input输入框展示两位小数之precision
- idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
- BUG destroyer!!Practical debugging skills are super comprehensive
- 关于出现大量close_wait状态的理解
- 开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
- The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'
猜你喜欢
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
【debug锦集】Expected input batch_size (1) to match target batch_size (0)
扫雷小游戏——C语言
mysql基础知识(二)
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
两个地址池r2负责管地址池r1负责管dhcp中继
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
Explanation of
BUG destroyer!!Practical debugging skills are super comprehensive
MySQL基础操作
随机推荐
【线性神经网络】softmax回归
三子棋的代码实现
Hand in hand to realize the picture preview plug-in (3)
WPF WPF 】 【 the depth resolution of the template
ENSP,划分VLAN、静态路由,三层交换机综合配置
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
已解决:不小心卸载pip后(手动安装pip的两种方式)
Heavyweight | The Open Atomic School Source Line activity was officially launched
qlib架构
MySQL database backup
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
MySQL to revise the root password
MySQL基础操作
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
重磅 | 基金会为白金、黄金、白银捐赠人授牌
PWN ROP
HCIP Day 10_BGP Route Summary Experiment