当前位置:网站首页>剑指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;
}
};
边栏推荐
- 《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
- unity2d小游戏
- 开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- type_traits metaprogramming library learning
- Safety 20220709
- three.js 制作3D相册
- Redis uses LIST to cache the latest comments
- 高等数学---第九章二重积分
- 重磅 | 开放原子校源行活动正式启动
猜你喜欢
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
$attrs/$listeners
C语言表白代码?
type_traits metaprogramming library learning
Daily practice of LeetCode - 138. Copy a linked list with random pointers
扫雷小游戏——C语言
Recursive implementation of the Tower of Hanoi problem
【C语言进阶】文件操作(一)
随机推荐
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
扫雷小游戏——C语言
【线性神经网络】softmax回归
微信小程序使用云函数更新和添加云数据库嵌套数组元素
Gaussian distribution and its maximum likelihood estimation
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
IDEA常用快捷键与插件
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
HCIP Day 10_BGP Route Summary Experiment
three.js 制作3D相册
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
MySQL修改root账号密码
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
unity2d小游戏
(树) 最近公共祖先(LCA)
【AUTOSAR-RTE】-4-Port and Interface and Data Type
qlib架构
C语言从入门到如土——数据的存储
Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code