当前位置:网站首页>剑指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;
}
};
边栏推荐
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- Can‘t load /home/Iot/.rnd into RNG
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- 重磅 | 开放原子校源行活动正式启动
- MySQL修改root账号密码
- The BP neural network
- Thinking about data governance after Didi fines
- (六)枚举、注解
- SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
- 重磅 | 基金会为白金、黄金、白银捐赠人授牌
猜你喜欢
XSS靶场(三)prompt to win
Daily practice of LeetCode - palindrome structure of OR36 linked list
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
"A daily practice, happy water problem" 1331. Array serial number conversion
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
STM32HAL库修改Hal_Delay为us级延时
Win10 CUDA CUDNN installation configuration (torch paddlepaddle)
HCIP Day 10_BGP Route Summary Experiment
微信小程序使用云函数更新和添加云数据库嵌套数组元素
手把手实现图片预览插件(三)
随机推荐
$attrs/$listeners
Can't load /home/Iot/.rnd into RNG
errno error code and meaning (Chinese)
C language from entry to such as soil, the data store
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
open failed: EACCES (Permission denied)
MySQL fuzzy query can use INSTR instead of LIKE
mysql基础知识(二)
MySQL based operations
Why don't you programmers make a living off your own projects?And have to work for someone else?
mysql数据库安装(详细)
Redis uses LIST to cache the latest comments
Explanation of
Unity2D 自定义Scriptable Tiles的理解与使用(四)——开始着手构建一个基于Tile类的自定义tile(下)
"A daily practice, happy water problem" 1331. Array serial number conversion
ClickHouse:设置远程连接
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
三子棋的代码实现
Safety 20220709
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem