代码随想录算法训练营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;
    }
};
相关推荐
历程里程碑1 小时前
Linux 库
java·linux·运维·服务器·数据结构·c++·算法
Sheep Shaun1 小时前
如何让一个进程诞生、工作、终止并等待回收?——探索Linux进程控制与Shell的诞生
linux·服务器·数据结构·c++·算法·shell·进程控制
Pluchon1 小时前
硅基计划4.0 简单模拟实现AVL树&红黑树
java·数据结构·算法
小龙报1 小时前
【51单片机】从 0 到 1 玩转 51 蜂鸣器:分清有源无源,轻松驱动它奏响新年旋律
c语言·数据结构·c++·stm32·单片机·嵌入式硬件·51单片机
dllxhcjla1 小时前
数据结构和算法
数据结构
历程里程碑2 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
sin_hielo2 小时前
leetcode 1653
数据结构·算法·leetcode
李日灐3 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
熬夜有啥好3 小时前
数据结构——排序与查找
数据结构
YuTaoShao3 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法二)排序 + 二分查找
数据结构·算法·leetcode