当前位置:网站首页>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;
}
};
边栏推荐
- exsl文件预览,word文件预览网页方法
- 手把手实现图片预览插件(三)
- ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
- Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
- STM32HAL library modifies Hal_Delay to us-level delay
- 【C语言进阶】文件操作(一)
- From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)
- Unity Fighter
猜你喜欢

扫雷游戏(c语言写)

Unity打灵狐者

STM32HAL库修改Hal_Delay为us级延时
![[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters](/img/98/282d8d192f701ad33de48aeeb038b4.png)
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters
![[C language] Detailed explanation of operators](/img/fa/dce3da39f19b51c6d1b682128da36b.png)
[C language] Detailed explanation of operators

input输入框展示两位小数之precision

MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)

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

PWN ROP

Recursive implementation of the Tower of Hanoi problem
随机推荐
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
PWN ROP
Why don't you programmers make a living off your own projects?And have to work for someone else?
【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
Go语学习笔记 - 处理超时问题 - Context使用 | 从零开始Go语言
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
pom文件成橘红色未加载的解决方案
prompt.ml/15中<svg>标签使用解释
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
MySQL based operations
C语言表白代码?
volatile内存语义以及实现 -volatile写和读对普通变量的影响
The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
【wpf】wpf中的那些模板之深度解析
【AUTOSAR-RTE】-4-Port and Interface and Data Type
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
unity2d game
Exsl file preview, word file preview web page method
exsl文件预览,word文件预览网页方法
From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)