层序遍历 10题
- 102.二叉树的层序遍历
- 107.二叉树的层次遍历II
- 199.二叉树的右视图
- 637.二叉树的层平均值
- 429.N叉树的层序遍历
- 515.在每个树行中找最大值
- 116.填充每个节点的下一个右侧节点指针
- 117.填充每个节点的下一个右侧节点指针II
- 104.二叉树的最大深度
- 111.二叉树的最小深度
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int> > result;
queue<TreeNode*> q;
if(root == nullptr) return result;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> ans;
while(size--){
TreeNode* cur = q.front();
q.pop();
ans.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
result.push_back(ans);
}
return result;
}
};
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int> > result;
queue<TreeNode*> q;
if(root == nullptr) return result;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> ans;
while(size--){
TreeNode* cur = q.front();
q.pop();
ans.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
result.push_back(ans);
}
reverse(result.begin(),result.end());
return result;
}
};
题目:199. 二叉树的右视图
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
queue<TreeNode*> q;
if(root == nullptr) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> ans;
while(size--){
TreeNode* cur = q.front();
q.pop();
ans.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(size == 0){
res.push_back(cur->val);
}
}
}
return res;
}
};
class Solution {
public:
vector<double> averageOfLevels(TreeNode* root) {
vector<double> res;
queue<TreeNode*> q;
if(root == nullptr) return res;
q.push(root);
while(!q.empty()){
int size = q.size();
double size2 = q.size();
long long sum = 0;
vector<int> ans;
while(size--){
TreeNode* cur = q.front();
q.pop();
ans.push_back(cur->val);
sum += cur->val;
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(size == 0){
double n = sum / size2;
res.push_back(n);
}
}
}
return res;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
vector<vector<int> > result;
queue<Node*> q;
if(root == nullptr) return result;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> ans;
while(size--){
Node * cur = q.front();
q.pop();
ans.push_back(cur->val);
vector<Node*> chil = cur->children;
for(int i = 0; i < chil.size(); ++i){
if(chil[i] != nullptr){
q.push(chil[i]);
}
}
}
result.push_back(ans);
}
return result;
}
};
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> result;
queue<TreeNode*> q;
if(root == nullptr) return result;
q.push(root);
while(!q.empty()){
int size = q.size();
int max = INT_MIN;
vector<int> ans;
while(size--){
TreeNode* cur = q.front();
q.pop();
ans.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(cur->val > max) max = cur->val;
if(size == 0){
result.push_back(max);
}
}
}
return result;
}
};
class Solution {
public:
Node* connect(Node* root) {
queue<Node*> q;
if(root == nullptr) return root;
q.push(root);
while(!q.empty()){
int size = q.size();
int max = INT_MIN;
vector<int> ans;
while(size--){
Node* cur = q.front();
q.pop();
ans.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(cur->val > max) max = cur->val;
if(size == 0){
cur->next = nullptr;
}else{
cur->next = q.front();
}
}
}
return root;
}
};
class Solution {
public:
int maxDepth(TreeNode* root) {
int ans = 0;
queue<TreeNode*> q;
if(root == nullptr) return 0;
q.push(root);
while(!q.empty()){
int size = q.size();
while(size--){
TreeNode* cur = q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
ans++;
}
return ans;
}
};
class Solution {
public:
int minDepth(TreeNode* root) {
int ans = 0;
queue<TreeNode*> q;
if(root == nullptr) return 0;
q.push(root);
while(!q.empty()){
int size = q.size();
while(size--){
TreeNode* cur = q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
if(cur->left == nullptr && cur->right == nullptr)return ans+1;
}
ans++;
}
return ans;
}
};
226.翻转二叉树
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == nullptr) return root;
if(root->left)
invertTree(root->left);
if(root->right)
invertTree(root->right);
swap(root->left,root->right);
return root;
}
};
101.对称二叉树
题目:101. 对称二叉树
class Solution {
public:
bool check(TreeNode* l, TreeNode* r){
if(l == nullptr && r == nullptr) return true;
else if(l == nullptr || r == nullptr) return false;
else if(l->val != r->val) return false;
return check(l->left,r->right) && check(l->right,r->left);
}
bool isSymmetric(TreeNode* root) {
return check(root->left,root->right);
}
};
题目:100. 相同的树
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p == nullptr && q == nullptr) return true;
else if(p == nullptr || q == nullptr) return false;
else if(p->val != q->val) return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};
题目:572. 另一棵树的子树
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(p == nullptr && q == nullptr) return true;
else if(p == nullptr || q == nullptr) return false;
else if(p->val != q->val) return false;
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if( isSameTree(root,subRoot) ) return true;
if(root->left) {
if (isSubtree(root->left,subRoot)) return true;
}
if(root->right) {
if ( isSubtree(root->right,subRoot) ) return true;
}
return false;
}
};
总结
题型:二叉树的层序遍历(队列),翻转二叉树,对称二叉树
技巧:层序遍历的相关题目,学会在队列每一层加上处理就很简单。
二叉树的相关题目思考出如何递归就很简单,递归三部曲要想好。
- 确定递归函数的参数和返回值
- 确定终止条件
- 确定单层递归的逻辑