当前位置:网站首页>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;
}
};
边栏推荐
- 问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
- qlib架构
- [shell basics] determine whether the directory is empty
- The third is the code to achieve
- Unity Fighter
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
- PCL 计算点云坐标最值及其索引
- Understanding of the presence of a large number of close_wait states
- pom文件成橘红色未加载的解决方案
- Unity打灵狐者
猜你喜欢
[Linear Neural Network] softmax regression
BUG消灭者!!实用调试技巧超全整理
【线性神经网络】softmax回归
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
重磅 | 开放原子校源行活动正式启动
XSS shooting range (3) prompt to win
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
WeChat applet uses cloud functions to update and add cloud database nested array elements
开源社区三十年 | 2022开放原子全球开源峰会开源社区三十年专题活动圆满召开
随机推荐
(6) Enumeration and annotation
递归实现汉诺塔问题
open failed: EACCES (Permission denied)
Hand in hand to realize the picture preview plug-in (3)
PWN ROP
ENSP,划分VLAN、静态路由,三层交换机综合配置
MySQL to revise the root password
Exsl file preview, word file preview web page method
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
三子棋的代码实现
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
【py脚本】批量二值化处理图像
[shell basics] determine whether the directory is empty
[C language] Detailed explanation of operators
Why don't you programmers make a living off your own projects?And have to work for someone else?
高斯分布及其极大似然估计
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem