二叉树的基本结构
#include<iostream>
using namespace std;
struct TreeNode {
/* data */
int val;
TreeNode* left;
TreeNode* right;
TreeNode() {}
TreeNode(int x) : left(nullptr), right(nullptr), val(x) {}
};
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/binary-tree-level-order-traversal/1. 层序遍历:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> queue;
if(root==nullptr) {
return res;
}
queue.push(root);
while(!queue.empty()) {
int n = queue.size();
vector<int> temp;
for(int i=0;i<n;i++) {
TreeNode* node = queue.front();
queue.pop();
temp.push_back(node->val);
if(node->left!=nullptr) {
queue.push(node->left);
}
if(node->right!=nullptr) {
queue.push(node->right);
}
}
res.push_back(temp);
}
return res;
}
};
2.力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/
二叉树的锯齿形层序遍历
基本的写法实现,重要的是要加一个 Flag 标值位,
每次来进行反转
/**
* 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>> res;
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
if(root==nullptr) {
return res;
}
helper(root);
return res;
}
void helper(TreeNode* root) {
if(root==nullptr) {
return;
}
queue<TreeNode*> queue2;
queue2.push(root);
int flag = 0;
while(!queue2.empty()) {
vector<int> temp;
int n = queue2.size();
for(int i=0;i<n;i++) {
TreeNode* node = queue2.front();
int val = node->val;
temp.push_back(val);
queue2.pop();
if(node->left!=nullptr) {
queue2.push(node->left);
}
if(node->right!=nullptr) {
queue2.push(node->right);
}
}
if(flag%2==1) {
reverse(temp.begin(),temp.end());
}
res.push_back(temp);
flag++;
}
}
};
3. 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台备战技术面试?力扣提供海量技术面试资源,帮助你高效提升编程技能,轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/description/
自底到上部的来层序遍历二叉树
将最后的结果逆转下即可得到最终结果
/**
* 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>> levelOrderBottom(TreeNode* root) {
auto res = vector<vector<int>>();
if(!root) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
auto level = vector<int>();
int size = q.size();
for(int i = 0; i < size; ++i) {
TreeNode* node = q.front();
// 弹出一个其中的元素。
q.pop();
level.push_back(node->val);
if(node->left!=nullptr) {
q.push(node->left);
}
if(node->right!=nullptr) {
q.push(node->right);
}
}
res.push_back(level);
}
reverse(res.begin(),res.end());
return res;
}
};
199. 二叉树的右视图https://leetcode.cn/problems/binary-tree-right-side-view/
先下结论:
遍历到最后的一个节点的时候,将最后的一个节点值加入进去
/**
* 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 {};
}
vector<int> res;
queue<TreeNode*> queue2;
queue2.push(root);
while (!queue2.empty()) {
int n = queue2.size();
for (int i=0;i<n;i++) {
TreeNode* node = queue2.front();
queue2.pop();
if(node->left!=nullptr) {
queue2.push(node->left);
}
if(node->right!=nullptr) {
queue2.push(node->right);
}
if(i==n-1) {
res.push_back(node->val);
}
}
}
return res;
}
};
637. 二叉树的层平均值https://leetcode.cn/problems/average-of-levels-in-binary-tree/
/**
* 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) {
vector<double> res;
if(!root) {
return res;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) { //
int size = q.size();
double sum2 = 0;
for(int i=0; i<size; i++) {
auto node = q.front();
q.pop();
sum2+=node->val;
if(node->left!=nullptr) {
q.push(node->left);//
}
if(node->right!=nullptr) {
q.push(node->right);
}
}
res.push_back(sum2/size);
}
return res;
}
};