代码随想录算法训练营第十五天| 二叉树的层序遍历十题 LeetCode226.翻转二叉树 101.对称二叉树 三题

层序遍历 10题

  • 102.二叉树的层序遍历
  • 107.二叉树的层次遍历II
  • 199.二叉树的右视图
  • 637.二叉树的层平均值
  • 429.N叉树的层序遍历
  • 515.在每个树行中找最大值
  • 116.填充每个节点的下一个右侧节点指针
  • 117.填充每个节点的下一个右侧节点指针II
  • 104.二叉树的最大深度
  • 111.二叉树的最小深度

题目:102. 二叉树的层序遍历

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;
    }
};

题目:107. 二叉树的层序遍历 II

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;
    }
};

题目:637. 二叉树的层平均值

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;
    }
};

题目:429. N 叉树的层序遍历

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;
    }
};

题目:515. 在每个树行中找最大值

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;
    }
};

题目:116. 填充每个节点的下一个右侧节点指针

题目:117. 填充每个节点的下一个右侧节点指针 II

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;
    }
};

题目:104. 二叉树的最大深度

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;
    }
};

题目:111. 二叉树的最小深度

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.翻转二叉树

题目:515. 在每个树行中找最大值

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;
    }
};

总结

题型:二叉树的层序遍历(队列),翻转二叉树,对称二叉树

技巧:层序遍历的相关题目,学会在队列每一层加上处理就很简单。

        二叉树的相关题目思考出如何递归就很简单,递归三部曲要想好。

  1. 确定递归函数的参数和返回值
  2. 确定终止条件
  3. 确定单层递归的逻辑
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值