目录
- 199. 二叉树的右视图
- 637. 二叉树的层平均值
- 102. 二叉树的层序遍历
- 103. 二叉树的锯齿形层序遍历
199. 二叉树的右视图
LeetCode_link
给定一个二叉树的 根节点 root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1:
输入: [1,2,3,null,5,null,4]
输出: [1,3,4]
示例 2:
输入: [1,null,3]
输出: [1,3]
示例 3:
输入: []
输出: []
提示:
二叉树的节点个数的范围是 [0,100]
-100 <= Node.val <= 100
思路:用队列
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
if(root == nullptr) return {};
queue<TreeNode*> q;
TreeNode* r = root;
vector<int> rec;
q.push(root);
while(!q.empty()){
TreeNode* node = q.front();
if(node->left != nullptr) q.push(node->left);
if(node->right != nullptr) q.push(node->right);
if(node == r){
rec.push_back(node->val);
r = q.back();
}
q.pop();
}
return rec;
}
};
637. 二叉树的层平均值
LeetCode_link
给定一个非空二叉树的根节点 root
, 以数组的形式返回每一层节点的平均值。与实际答案相差 10^-5
以内的答案可以被接受。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[3.00000,14.50000,11.00000]
解释:第 0 层的平均值为 3,第 1 层的平均值为 14.5,第 2 层的平均值为 11 。
因此返回 [3, 14.5, 11] 。
示例 2:
输入:root = [3,9,20,15,7]
输出:[3.00000,14.50000,11.00000]
提示:
树中节点数量在 [1, 10^4]
范围内
-2^31 <= Node.val <= 2^31 - 1
思路:用队列
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
queue<TreeNode*> q;
TreeNode* r = root;
q.push(root);
int count = 0;
double sum = 0;
vector<double> rec;
while(!q.empty()){
TreeNode* node = q.front();
if(node->left != nullptr) q.push(node->left);
if(node->right != nullptr) q.push(node->right);
count ++;
sum += node->val;
if(node == r){
rec.push_back(sum / count);
sum = 0;
count = 0;
r = q.back();
}
q.pop();
}
return rec;
}
};
102. 二叉树的层序遍历
LeetCode_link
给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000]
内
-1000 <= Node.val <= 1000
思路:队列
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root == nullptr) return {};
queue<TreeNode*> q;
TreeNode* r = root;
q.push(root);
vector<vector<int>> rec;
vector<int> temp;
while(!q.empty()){
TreeNode* node = q.front();
if(node->left != nullptr) q.push(node->left);
if(node->right != nullptr) q.push(node->right);
temp.push_back(node->val);
if(node == r){
rec.push_back(temp);
temp.clear();
r = q.back();
}
q.pop();
}
return rec;
}
};
103. 二叉树的锯齿形层序遍历
LeetCode_link
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[20,9],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
树中节点数目在范围 [0, 2000]
内
-100 <= Node.val <= 100
思路:先层次遍历,之后再考虑翻转
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
if(root == nullptr) return {};
queue<TreeNode*> q;
TreeNode* r = root;
q.push(root);
vector<vector<int>> rec;
vector<int> temp;
while(!q.empty()){
TreeNode* node = q.front();
if(node->left != nullptr) q.push(node->left);
if(node->right != nullptr) q.push(node->right);
temp.push_back(node->val);
if(node == r){
rec.push_back(temp);
temp.clear();
r = q.back();
}
q.pop();
}
vector<vector<int>> recc;
for(int i = 0; i < rec.size(); i++){
if(i % 2 == 1){
recc.push_back(reserve(rec[i]));
}else{
recc.push_back(rec[i]);
}
}
return recc;
}
vector<int> reserve(vector<int> t){
int left = 0, right = t.size()-1;
while(left < right){
swap(t[left], t[right]);
left ++;
right --;
}
return t;
}
};