目录
429. N 叉树的层序遍历
解析
题解
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
// 74.专题十三_队列+宽搜_N 叉树的层序遍历_C++
vector<vector<int>> ret; // 记录最终结果
queue<Node*> q;
if(root == nullptr) return ret;
q.push(root);
while(q.size())
{
int sz = q.size(); // 先求出本层元素的个数
vector<int> tmp;
for(int i = 0; i < sz; ++i)
{
Node* t = q.front();
q.pop();
tmp.push_back(t->val);
for(Node* child : t->children) // 让下一层节点入队
{
if(child != nullptr)
q.push(child);
}
}
ret.push_back(tmp);
}
return ret;
}
};
103. 二叉树的锯齿形层序遍历
解析
题解
/**
* 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) {
// 75.专题十三_队列+宽搜_二叉树的锯齿形层序遍历_C++
vector<vector<int>> ret;
queue<TreeNode*> q;
int floor = 1;
if(root == nullptr) return ret;
q.push(root);
while(q.size())
{
vector<int> tmp;
int sz = q.size();
for(int i = 0; i < sz; ++i)
{
TreeNode* t = q.front();
q.pop();
tmp.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
if(floor % 2 == 0) // 判断是否需要逆序
reverse(tmp.begin(), tmp.end());
ret.push_back(tmp);
floor++;
}
return ret;
}
};
662. 二叉树最大宽度
解析
题解
/**
* 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:
int widthOfBinaryTree(TreeNode* root) {
// 76.专题十三_队列+宽搜_二叉树最大宽度_C++
vector<pair<TreeNode*, unsigned int>> q; // 用数组模拟队列
q.push_back({root, 1});
unsigned int ret = 0;
while(q.size())
{
// 先更新这一层
auto& [x1, y1] = q[0];
auto& [x2, y2] = q.back();
ret = max(ret, y2 - y1 + 1);
// 让下一层进队
vector<pair<TreeNode*, unsigned int>> tmp; // 让下一层进入这个队列
for(auto& [x, y] : q)
{
if(x->left) tmp.push_back({x->left, y * 2});
if(x->right) tmp.push_back({x->right, y * 2 + 1});
}
q = tmp;
}
return ret;
}
};
515. 在每个树行中找最大值
解析
题解
/**
* 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> largestValues(TreeNode* root) {
// 77.专题十三_队列+宽搜_在每个树行中找最大值_C++
vector<int> ret;
if(root == nullptr) return ret;
queue<TreeNode*> q;
q.push(root);
while(q.size())
{
int sz = q.size();
int biggest = INT_MIN;
for(int i = 0; i < sz; ++i)
{
TreeNode* tmp = q.front();
q.pop();
biggest = max(biggest, tmp->val);
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
ret.push_back(biggest);
}
return ret;
}
};