文章目录
本文说明&前置知识
有10道题可以使用层序遍历进行解决,因为当时顺手做了,所以在这里记录一下。
就当新开一个专题了。
这些题可以使用层次遍历做,但是还有很多其他方法(甚至可能更适合),但是这里就只使用层次遍历做了。
参考文章:
LeetCode刷题笔记【9】:二叉树专题-1(分别用递归遍历、迭代遍历、标记遍历实现前、中、后序遍历)
LeetCode刷题笔记【10】:二叉树专题-2(二叉树的层序遍历、翻转二叉树、对称二叉树)
102. 二叉树的层序遍历
题目描述
LeetCode链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/
解题思路
思路: 创建一个队列, 存储每一层的元素;
在每一次循环时遍历当前队列中的所有元素, 将其val加入新的vector, 将其子节点入队列
代码
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
vector<int> curLevel;
int size = q.size();
for(int i=0; i<size; ++i){
TreeNode* curNode = q.front();
q.pop();
curLevel.push_back(curNode->val);
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
}
ans.push_back(curLevel);
}
return ans;
}
};
107. 二叉树的层序遍历 II
题目描述
LeetCode链接:https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/description/
解题思路
类比《102. 二叉树的层序遍历》, 最后把ans reverse一下就行了
代码
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
vector<int> curLevel;
int size = q.size();
for(int i=0; i<size; ++i){
TreeNode* curNode = q.front();
q.pop();
curLevel.push_back(curNode->val);
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
}
ans.push_back(curLevel);
}
reverse(ans.begin(), ans.end());
return ans;
}
};
199. 二叉树的右视图
题目描述
LeetCode链接:https://leetcode.cn/problems/binary-tree-right-side-view/description/
解题思路
类比《102. 二叉树的层序遍历》, 只需要选取每一层的最后一个元素加入ans即可
代码
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i=0; i<size; ++i){
TreeNode* cur = q.front();
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
if(i==size-1)
ans.push_back(cur->val);
}
}
return ans;
}
};
637. 二叉树的层平均值
题目描述
LeetCode链接:https://leetcode.cn/problems/average-of-levels-in-binary-tree/description/
解题思路
类比《102. 二叉树的层序遍历》, 设置一个sum, 最后在ans中加入sum/size即可
代码
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
double sum=0;
int size = q.size();
for(int i=0; i<size; ++i){
TreeNode* curNode = q.front();
q.pop();
sum += curNode->val;
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
}
ans.push_back(sum/size);
}
return ans;
}
};
429. N 叉树的层序遍历
题目描述
LeetCode链接:https://leetcode.cn/problems/n-ary-tree-level-order-traversal/description/
解题思路
类比《102. 二叉树的层序遍历》, 其实是一样的
代码
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int>> ans;
if(root==NULL)
return ans;
queue<Node*> q;
q.push(root);
while(!q.empty()){
vector<int> curLevel;
int size = q.size();
for(int i=0; i<size; ++i){
Node* curNode = q.front();
q.pop();
curLevel.push_back(curNode->val);
for(auto child : curNode->children)
q.push(child);
}
ans.push_back(curLevel);
}
return ans;
}
};
515. 在每个树行中找最大值
题目描述
LeetCode链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row/description/
解题思路
类比《102. 二叉树的层序遍历》, 只需要选取每一层的max即可
代码
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
int maxNum = INT_MIN;
for(int i=0; i<size; ++i){
TreeNode* cur = q.front();
q.pop();
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
maxNum = max(maxNum, cur->val);
}
ans.push_back(maxNum);
}
return ans;
}
};
116. 填充每个节点的下一个右侧节点指针
题目描述
LeetCode链接:https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/description/
解题思路
类比《102. 二叉树的层序遍历》, 只需要在过程中修改即可
代码
class Solution {
public:
Node* connect(Node* root) {
if(root==NULL)
return root;
queue<Node*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i=0; i<size; ++i){
Node* curNode = q.front();
q.pop();
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
if(i!=size-1)
curNode->next = q.front();
}
}
return root;
}
};
117. 填充每个节点的下一个右侧节点指针 II
题目描述
LeetCode链接:https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/description/
解题思路
本题和上一题其实是一样的.
类比《102. 二叉树的层序遍历》, 只需要在过程中修改即可
代码
class Solution {
public:
Node* connect(Node* root) {
if(root==NULL)
return root;
queue<Node*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
for(int i=0; i<size; ++i){
Node* curNode = q.front();
q.pop();
if(curNode->left)
q.push(curNode->left);
if(curNode->right)
q.push(curNode->right);
if(i!=size-1)
curNode->next = q.front();
}
}
return root;
}
};
104. 二叉树的最大深度
题目描述
LeetCode链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/
层次遍历法
思路1: 层次遍历(广度优先遍历), 统计层数
class Solution {
public:
int maxDepth(TreeNode* root) {
int ans=0;
if(root==NULL)
return ans;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
ans++;
int size = que.size();
for(int i=0; i<size; ++i){
TreeNode* cur = que.front();
que.pop();
if(cur->left)
que.push(cur->left);
if(cur->right)
que.push(cur->right);
}
}
return ans;
}
};
递归法
思路2: 递归深度优先遍历, 在递归函数中加index+1, 求max
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL)
return 0;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
111. 二叉树的最小深度
题目描述
LeetCode链接:https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
层序遍历
思路1: 层次遍历(广度优先遍历), 统计层数
class Solution {
public:
int minDepth(TreeNode* root) {
int ans=0;
if(root==NULL)
return ans;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
ans++;
int size = que.size();
for(int i=0; i<size; ++i){
TreeNode* cur = que.front();
if(cur->left==NULL && cur->right==NULL)// 在这里检查一下cur是不是叶节点, 只要发现叶子节点就返回
return ans;
que.pop();
if(cur->left)
que.push(cur->left);
if(cur->right)
que.push(cur->right);
}
}
return ans;
}
};
递归
思路2: 递归深度优先遍历, 在递归函数中加index+1, 求max
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL)
return 0;
if(root->left==NULL && root->right==NULL)
return 1;
if(root->left!=NULL && root->right!=NULL)
return min(minDepth(root->left), minDepth(root->right)) + 1;
if(root->left==NULL && root->right!=NULL)
return minDepth(root->right) + 1;
if(root->left!=NULL && root->right==NULL)
return minDepth(root->left) + 1;
return 0;
}
};
递归的优化
感觉题解中的深度遍历优雅很多, 模仿一下
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==NULL)
return 0;
if(root->left==NULL && root->right==NULL)
return 1;
int ans = INT_MAX;
if(root->right!=NULL)
ans = min(ans, minDepth(root->right)+1);
if(root->left!=NULL)
ans = min(ans, minDepth(root->left)+1);
return ans;
}
};