当前位置:网站首页>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;
}
};
边栏推荐
- Create componentized development based on ILRuntime hot update
- VScode+ESP32快速安装ESP-IDF插件
- [py script] batch binarization processing images
- Bubble sort, selection sort, insertion sort, binary search directly
- .NET-6.WinForm2.NanUI学习和总结
- 【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
- MySQL修改root账号密码
- [C language] General method of base conversion
- Basic knowledge of mysql (2)
- Thinking about data governance after Didi fines
猜你喜欢

On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held

ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches

Unity手机游戏性能优化系列:针对CPU端的性能调优

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

MySQL database must add, delete, search and modify operations (CRUD)

MySQL数据库必会的增删查改操作(CRUD)

HCIP Day 10_BGP Route Summary Experiment

C语言表白代码?

Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
随机推荐
打造基于ILRuntime热更新的组件化开发
手把手实现图片预览插件(三)
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
Heavyweight | The Open Atomic School Source Line activity was officially launched
三道leetcode上的oj题
强化学习:从入门到入坑再到拉屎
VScode+ESP32 quickly install ESP-IDF plugin
Unity Fighter
STM32HAL库修改Hal_Delay为us级延时
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
扫雷游戏(c语言写)
Unity shader forge和自带的shader graph,有哪些优缺点?
Learning DAVID Database (1)
BUG destroyer!!Practical debugging skills are super comprehensive
Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
(6) Enumeration and annotation
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
微信小程序使用云函数更新和添加云数据库嵌套数组元素