代码随想录算法训练营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;
    }
};
相关推荐
木子02042 小时前
Java8集合list.parallelStream() 和 list.stream() 区别
数据结构·list
Z1Jxxx3 小时前
0和1的个数
数据结构·c++·算法
ldccorpora3 小时前
Chinese News Translation Text Part 1数据集介绍,官网编号LDC2005T06
数据结构·人工智能·python·算法·语音识别
AuroraWanderll3 小时前
类和对象(六)--友元、内部类与再次理解类和对象
c语言·数据结构·c++·算法·stl
leaves falling4 小时前
c语言-给定两个数,求这两个数的最大公约数
数据结构·算法
想做后端的小C4 小时前
数据结构:线性表原地逆置
数据结构·考研
jikiecui4 小时前
信奥崔老师:三目运算 (Ternary Operator)
数据结构·c++·算法
无限进步_4 小时前
【C语言&数据结构】另一棵树的子树:递归思维的双重奏
c语言·开发语言·数据结构·c++·算法·github·visual studio