算法训练营day15

总结

往二维vector里push_back时,可以直接push一个一维vector进去;

vector<vector<int>> ans;
vector<int> V;
ans.push_back(V);

一个函数有返回值的时候,最后一定要return,经常不写是因为void不用return;

二叉树的最大深度就是问有几层,二叉树的最小深度就是看哪层出现叶子节点,都用层序遍历;

二叉树考虑遍历方式的时候,若递归需要返回值,看一下哪种方式可以把所需信息传给上一层;

需要收集孩子的信息,向中间节点传递的时候,用后序遍历;

102.二叉树的层序遍历

这里要求分层,所以每次都统计一下size;

  1. 如果根不为空,把根放到队列;
  2. while(不空)先统计队列里元素数量size,创建一个vector;
  3. 循环size,存弹队头,放vector里,扩展孩子;
  4. 把vector放到二维ans里;
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            vector<int> V;
            int size=q.size();
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                V.push_back(t->val);

                if(t->left!=nullptr) q.push(t->left);
                if(t->right!=nullptr) q.push(t->right);
            }
            ans.push_back(V);
        }
        return ans;
    }
};

107. 二叉树的层序遍历 II

正向的层序遍历,最后反转一下ans;

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            vector<int> V;
            int size=q.size();
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                V.push_back(t->val);

                if(t->left!=nullptr) q.push(t->left);
                if(t->right!=nullptr) q.push(t->right);
            }
            ans.push_back(V);
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

199. 二叉树的右视图

只把每层最后一个存到ans里;

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        queue<TreeNode*> q;
        vector<int> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            int size=q.size();
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                if(i==size-1) ans.push_back(t->val);

                if(t->left!=nullptr) q.push(t->left);
                if(t->right!=nullptr) q.push(t->right);
            }
        }
        return ans;
    }
};

637.二叉树的层平均值

遍历每层的时候,计算一下平均值;

class Solution {
public:
    vector<double> averageOfLevels(TreeNode* root) {
        queue<TreeNode*> q;
        vector<double> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            double sum=0;
            int size=q.size();
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                sum+=t->val;

                if(t->left!=nullptr) q.push(t->left);
                if(t->right!=nullptr) q.push(t->right);
            }
            ans.push_back(sum/size);
        }
        return ans;        
    }
};

429.N叉树的层序遍历

每个节点有个孩子vector,扩展的时候,把孩子vector遍历一遍;

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        queue<Node*> q;
        vector<vector<int>> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            int size=q.size();
            vector<int> V;
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                V.push_back(t->val);

                for(int i=0;i<t->children.size();i++){
                    if(t->children[i]) q.push(t->children[i]);
                }
            }
            ans.push_back(V);
        }
        return ans;        
    }
};

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

对于每层来说,更新最大值,再放进去就行;

class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        queue<TreeNode*> q;
        vector<int> ans;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            int size=q.size();
            long long mmax=-1e10;
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();
                mmax=max(mmax,(long long)t->val);

                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            ans.push_back(mmax);
        }
        return ans; 
    }
};

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

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> q;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            int size=q.size();
            Node* nodepre;
            Node* t;
            for(int i=0;i<size;i++){
                if(i==0){
                    t=q.front();
                    q.pop();
                    nodepre=t;
                }
                else{
                    t=q.front();
                    q.pop();
                    nodepre->next=t;
                    nodepre=nodepre->next;                    
                }
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
                nodepre->next=NULL;
            }
        }
        return root;        
    }
};

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

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> q;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            int size=q.size();
            Node* nodepre;
            Node* t;
            for(int i=0;i<size;i++){
                if(i==0){
                    t=q.front();
                    q.pop();
                    nodepre=t;
                }
                else{
                    t=q.front();
                    q.pop();
                    nodepre->next=t;
                    nodepre=nodepre->next;                    
                }
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
                nodepre->next=NULL;
            }
        }
        return root;        
    }
};

104.二叉树的最大深度

其实就是有多少层;

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int ans=0;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            ans++;
            int size=q.size();
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();

                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
        }
        return ans; 
    }
};

111.二叉树的最小深度

层序遍历,当前层存在叶子节点时,就说明当前层的深度是最小深度;

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int ans=0;
        if(root!=nullptr) q.push(root);
        while(!q.empty()){
            ans++;
            int size=q.size();
            bool flag=false;
            for(int i=0;i<size;i++){
                auto t=q.front();
                q.pop();

                if(t->left==NULL&&t->right==NULL){
                    flag=true;
                    break;
                }
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            if(flag) break;
        }
        return ans; 
    }
};

226.翻转二叉树

递归遍历,每个节点swap俩孩子就行;

class Solution {
public:
    TreeNode* dfs(TreeNode* t){
        if(t==nullptr) return t;
        swap(t->left,t->right);
        dfs(t->left);
        dfs(t->right);
        return t;
    }

    TreeNode* invertTree(TreeNode* root) {
        TreeNode* ans=dfs(root);
        return ans;
    }
};

101.对称二叉树

首先,这道题要求了至少有一个节点,所以函数里不用判断root为空的情况;

当一个节点的左右子树是对称的,这个节点为根的子树就是对称二叉树,因此需要孩子给中间节点反馈信息,采用后序遍历;

当左右不为空且数值相等的时候,递归左子树左孩子右子树右孩子(外侧),左右右左(内侧),都为true才行;

当左右都为空的时候直接return true;

其他情况都是false;

class Solution {
public:

    bool compare(TreeNode*l,TreeNode*r){
        if(l!=nullptr&&r!=nullptr&&l->val==r->val){
            bool t1=compare(l->left,r->right);
            bool t2=compare(l->right,r->left);
            return t1&&t2;
        }
        else if(l==nullptr&&r==nullptr) return true;
        else return false;
    }

    bool isSymmetric(TreeNode* root) {
        return compare(root->left,root->right);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值