当前位置:网站首页>剑指offer专项突击版第15天
剑指offer专项突击版第15天
2022-07-31 04:29: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;
}
};
层序遍历,每次记录一层序列如果有下一层则清除当前序列并开始记录下一层序列,最终返回第一个就是最底层最左边的结点了
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. 二叉树的右侧视图
层序遍历记录每一层最后一个即可
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;
}
};
边栏推荐
- Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
- ClickHouse: Setting up remote connections
- MySQL数据库必会的增删查改操作(CRUD)
- MySQL数据库增删改查(基础操作命令详解)
- 递归实现汉诺塔问题
- binom二项分布,
- HCIP第十天_BGP路由汇总实验
- 【C语言进阶】文件操作(一)
- 强化学习:从入门到入坑再到拉屎
- C language from entry to such as soil, the data store
猜你喜欢

The idea project obviously has dependencies, but the file is not displayed, Cannot resolve symbol 'XXX'

【C语言进阶】文件操作(一)

递归实现汉诺塔问题

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

WPF WPF 】 【 the depth resolution of the template

扫雷小游戏——C语言

Hand in hand to realize the picture preview plug-in (3)

BP神经网络

mysql数据库安装(详细)

手把手实现图片预览插件(三)
随机推荐
Can‘t load /home/Iot/.rnd into RNG
微软 AI 量化投资平台 Qlib 体验
input输入框展示两位小数之precision
Reinforcement learning: from entry to pit to shit
Can't load /home/Iot/.rnd into RNG
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
WeChat applet uses cloud functions to update and add cloud database nested array elements
【论文阅读】Mastering the game of Go with deep neural networks and tree search
Hand in hand to realize the picture preview plug-in (3)
The BP neural network
interprocess communication
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
Redis uses sorted set to cache latest comments
Exsl file preview, word file preview web page method
微信小程序使用云函数更新和添加云数据库嵌套数组元素
(树) 最近公共祖先(LCA)
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held