算法刷题-二叉树

算法刷题-二叉树

144. 二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

思路

前序遍历:中左右

使用栈的时候要注意:先放右儿子,再放左儿子,再放中间,这样可以保证出栈的时候:中左右

放自己的时候,需要放一个空指针作为标记

代码

递归版本

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> preorderTraversal(TreeNode* root) {
        vector<int> res;
        dfs(root,res);
        return res;
    }

    void dfs(TreeNode *root ,vector<int> &res){
        if(root==nullptr) return;
        res.push_back(root->val);
        dfs(root->left,res),dfs(root->right,res);
    }
};

栈版本

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> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){
            auto t=st.top();
            st.pop();
            if(t!=nullptr){
                //中左右
                if(t->right) st.push(t->right);
                if(t->left) st.push(t->left);
                st.push(t),st.push(nullptr);
            }else{
                t=st.top();
                st.pop();
                res.push_back(t->val);
            }
        }
        return res;
    }
};

145. 二叉树的后序遍历

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历

思路

后序遍历:左右中

使用栈遍历的时候,先放自己,在右儿子,在左儿子。这样可以保证出栈的时候顺序为:左右中

代码

递归版本

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> postorderTraversal(TreeNode* root) {
        vector<int> res;
        dfs(root,res);
        return res;
    }

    void dfs(TreeNode *root,vector<int> &res){
        if(root==nullptr) return;
        dfs(root->left,res),dfs(root->right,res);
        res.push_back(root->val);
    }
};

栈版本:

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> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){
            auto t=st.top();
            st.pop();
            if(t==nullptr){
                t=st.top();
                st.pop();
                res.push_back(t->val);
            }else{
                //左右中
                st.push(t),st.push(nullptr);
                if(t->right)st.push(t->right);
                if(t->left)st.push(t->left);
            }
        }
        return res;
    }
};

94. 二叉树的中序遍历

给定一个二叉树的根节点 root ,返回 它的 中序 遍历

思路

中序遍历:左中右

在使用栈遍历的时候,先让右儿子,再放左儿子,再放中间,这样可以保证出来的时候顺序为:左中右

放自己的时候,再放一个空指针作为标记,下次遍历到空指针的时候,下一个就是自己

代码

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> inorderTraversal(TreeNode* root) {
        vector<int> res;
        dfs(root,res);
        return res;
    }
    void dfs(TreeNode *root,vector<int> &res){
        if(root==nullptr) return;
        dfs(root->left,res);
        res.push_back(root->val);
        dfs(root->right,res);
    }
};

栈版本

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> inorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){
            auto t=st.top();
            if(t!=nullptr){
                st.pop();
                if(t->right) st.push(t->right);
                st.push(t);
                st.push(nullptr);
                if(t->left) st.push(t->left);
            }else{
                st.pop();
                t=st.top();
                st.pop();
                res.push_back(t->val);
            }
        }
        return res;
    }
};

102. 二叉树的层序遍历

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

思路

使用队列进行广度优先搜索,每次需要push_back的vector的大小应该根据上一次还剩余的队列的长度进行计算。

代码

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>> res;
        queue<TreeNode*> q;
        if(root!=nullptr) q.push(root);
        while(q.size()){
            int sz=q.size();
            vector<int> cur;
            while(sz--){
                auto t=q.front();
                q.pop();
                cur.push_back(t->val);
                if(t->left)q.push(t->left);
                if(t->right)q.push(t->right);
            }
            res.push_back(cur);
        }
        return res;

    }
};

107. 二叉树的层序遍历 II

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

思路

在上一题的基础上,只需要将从上往下遍历的结果reverse一下即可。

代码

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) {
        queue<TreeNode*> q;
        vector<vector<int>> res;
        if(root!=nullptr) q.push(root);
        while(q.size()){
            int sz=q.size();
            vector<int> cur;
            while(sz--){
                auto t=q.front();
                q.pop();
                cur.push_back(t->val);
                if(t->left) q.push(t->left);
                if(t->right) q.push(t->right);
            }
            res.push_back(cur);
        }
        reverse(res.begin(),res.end());
        return res;
    }
};
相关推荐
曼巴UE58 分钟前
UE 客户端 需要的网络同步概念总结
网络·c++·ue5
『昊纸』℃19 分钟前
C语言学习心得集合 篇1
c语言·算法·编程基础·学习心得·实践操作
云深麋鹿20 分钟前
C++ | 继承
开发语言·c++
小辉同志25 分钟前
Epoll+线程池
开发语言·c++·c·线程池·epoll
史迪仔011225 分钟前
[QML] Qt Quick Dialogs 模块使用指南
开发语言·前端·c++·qt
Chase_______26 分钟前
LeetCode 1456:定长子串中元音的最大数目
算法·leetcode
小O的算法实验室26 分钟前
2026年IEEE IOTJ,DNA序列启发相似性驱动粒子群算法+无人机与基站部署,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
谭欣辰26 分钟前
Floyd算法:动态规划解最短路径
c++·算法·图论
计算机安禾27 分钟前
【Linux从入门到精通】第12篇:进程的前后台切换与信号控制
linux·运维·算法
6Hzlia30 分钟前
【Hot 100 刷题计划】 LeetCode 84. 柱状图中最大的矩形 | C++ 两次单调栈基础扫法
c++·算法·leetcode