代码随想录算法训练营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;
    }
};
相关推荐
范纹杉想快点毕业几秒前
STM32单片机与ZYNQ PS端 中断+状态机+FIFO 综合应用实战文档(初学者版)
linux·数据结构·数据库·算法·mongodb
多米Domi0112 小时前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
L_090711 小时前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
划破黑暗的第一缕曙光15 小时前
[数据结构]:5.二叉树链式结构的实现1
数据结构
青桔柠薯片15 小时前
数据结构:单向链表,顺序栈和链式栈
数据结构·链表
XiaoFan01215 小时前
将有向工作流图转为结构树的实现
java·数据结构·决策树
睡一觉就好了。16 小时前
快速排序——霍尔排序,前后指针排序,非递归排序
数据结构·算法·排序算法
齐落山大勇16 小时前
数据结构——单链表
数据结构
皮皮哎哟16 小时前
深入浅出双向链表与Linux内核链表 附数组链表核心区别解析
c语言·数据结构·内核链表·双向链表·循环链表·数组和链表的区别
wWYy.17 小时前
指针与引用区别
数据结构