代码随想录算法训练营DAY13第六章 二叉树part01

目录

[144. 二叉树的前序遍历](#144. 二叉树的前序遍历)

[145. 二叉树的后序遍历](#145. 二叉树的后序遍历)

[94. 二叉树的中序遍历](#94. 二叉树的中序遍历)

[102. 二叉树的层序遍历](#102. 二叉树的层序遍历)

[107. 二叉树的层序遍历 II](#107. 二叉树的层序遍历 II)

[199. 二叉树的右视图](#199. 二叉树的右视图)

[637. 二叉树的层平均值](#637. 二叉树的层平均值)

[429. N 叉树的层序遍历](#429. N 叉树的层序遍历)

[515. 在每个树行中找最大值](#515. 在每个树行中找最大值)

[116. 填充每个节点的下一个右侧节点指针](#116. 填充每个节点的下一个右侧节点指针)

[117. 填充每个节点的下一个右侧节点指针 II](#117. 填充每个节点的下一个右侧节点指针 II)

[104. 二叉树的最大深度](#104. 二叉树的最大深度)

[111. 二叉树的最小深度](#111. 二叉树的最小深度)


144. 二叉树的前序遍历

cpp 复制代码
/**
 * 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 {
    void search(TreeNode* root,vector<int>& res){
        if(root==nullptr)return ;
        res.push_back(root->val);
        search(root->left,res);
        search(root->right,res);
    }
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        search(root,res);
        return res;
    }
};

145. 二叉树的后序遍历

cpp 复制代码
/**
 * 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 {
    void search(TreeNode* root,vector<int>& ans){
        if(root==nullptr)return ;
        search(root->left,ans);
        search(root->right,ans);
        ans.push_back(root->val);
    }
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        search(root,ans);
        return ans;
    }
};

94. 二叉树的中序遍历

cpp 复制代码
/**
 * 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 {
    void search(TreeNode* root,vector<int>& ans){
        if(root==nullptr)return ;
        search(root->left,ans);
        ans.push_back(root->val);
        search(root->right,ans);
    }
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> ans;
        search(root,ans);
        return ans;
    }
};

102. 二叉树的层序遍历

cpp 复制代码
/**
 * 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>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        queue<TreeNode* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            vector<int> path;
            for(int i=0;i<len;i++){
                TreeNode* node=q.front();
                q.pop();
                path.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            ans.push_back(path);
        }
        return ans;
    }
};

107. 二叉树的层序遍历 II

cpp 复制代码
/**
 * 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>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> ans;
        queue<TreeNode* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            vector<int> path;
            for(int i=0;i<len;i++){
                TreeNode* node=q.front();
                q.pop();
                path.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            ans.push_back(path);
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

199. 二叉树的右视图

cpp 复制代码
/**
 * 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> rightSideView(TreeNode* root) {
        vector<int> ans;
        queue<TreeNode* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            for(int i=0;i<len;i++){
                TreeNode* node=q.front();
                q.pop();
                if(i+1==len)ans.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
        }
        return ans;
    }
};

637. 二叉树的层平均值

cpp 复制代码
/**
 * 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<double> averageOfLevels(TreeNode* root) {
        vector<vector<int>> ans;
        queue<TreeNode* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            vector<int> path;
            for(int i=0;i<len;i++){
                TreeNode* node=q.front();
                q.pop();
                path.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            ans.push_back(path);
        }
        vector<double> x;
        for(int i=0;i<ans.size();i++){
            double sum=0;
            for(int j=0;j<ans[i].size();j++){
                sum+=ans[i][j];
            }
            sum/=ans[i].size();
            x.push_back(sum);
        }
        return x;
    }
};

429. N 叉树的层序遍历

cpp 复制代码
/*
// 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) {
        vector<vector<int>> ans;
        queue<Node* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            vector<int> path;
            for(int i=0;i<len;i++){
                Node* node=q.front();
                q.pop();
                path.push_back(node->val);
                for(int i=0;i<node->children.size();i++){
                    if(node->children[i])q.push(node->children[i]);
                }
            }
            ans.push_back(path);
        }
        return ans;
    }
};

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

cpp 复制代码
/**
 * 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) {
        vector<vector<int>> ans;
        queue<TreeNode* >q;
        if(root)q.push(root);
        while(!q.empty()){
            int len=q.size();
            vector<int> path;
            for(int i=0;i<len;i++){
                TreeNode* node=q.front();
                q.pop();
                path.push_back(node->val);
                if(node->left)q.push(node->left);
                if(node->right)q.push(node->right);
            }
            ans.push_back(path);
        }
        vector<int> x;
        for(int i=0;i<ans.size();i++){
            int t=ans[i][0];
            for(int j=0;j<ans[i].size();j++){
                t=max(ans[i][j],t);
            }
            x.push_back(t);
        }
        return x;
    }
};

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

cpp 复制代码
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

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

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

cpp 复制代码
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

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

104. 二叉树的最大深度

cpp 复制代码
/**
 * 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 maxDepth(TreeNode* root) {
        if(root==0)return 0;
        int left=maxDepth(root->left);
        int right=maxDepth(root->right);
        return max(left,right)+1;
    }
};

111. 二叉树的最小深度

cpp 复制代码
/**
 * 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 minDepth(TreeNode* root) {
        if(!root)return 0;
        if(!root->left&&!root->right)return 1;
        int deep=INT_MAX;
        if(root->left)deep=min(minDepth(root->left),deep);
        if(root->right)deep=min(minDepth(root->right),deep);
        return deep+1;
    }
};
相关推荐
CSharp精选营4 天前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假7 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠8 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦15 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠16 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾16 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82116 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q16 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒16 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
WL学习笔记16 天前
单项不带头不循环链表
数据结构·链表