<LeetCode>二叉树前/中/后/层遍历**递归&&非递归**

144. 二叉树的前序遍历

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

非递归 解法

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

94. 二叉树的中序遍历

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

非递归 解法

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

145. 二叉树的后序遍历

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

非递归 解法

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) {
        if(root==nullptr)return {};
        stack<TreeNode*>st;
        vector<int>ans;
        st.push(root);
        while(!st.empty() ){
            TreeNode*temp=st.top();
            ans.push_back(temp->val);
            st.pop();
            if(temp->left)st.push(temp->left);
            if(temp->right)st.push(temp->right);
        }
        reverse(ans.begin(),ans.end() );
        return ans;
    }
};

102. 二叉树的层序遍历

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) {
        if (root == nullptr)
            return {};
        queue<TreeNode*> que;
        que.push(root);
        vector<vector<int>> ans;
        while (!que.empty()) {
            vector<int> arr;
            int N = que.size();
            while (N--) {
                TreeNode* temp = que.front();
                que.pop();
                arr.push_back(temp->val);
                if (temp->left)
                    que.push(temp->left);
                if (temp->right)
                    que.push(temp->right);
            }
            ans.push_back(arr);
        }
        return ans;
    }
};
相关推荐
汀、人工智能2 小时前
[特殊字符] 第56课:在排序数组中查找元素的首末位置
数据结构·算法·数据库架构·图论·bfs·在排序数组中查找元素的首末位置
小O的算法实验室2 小时前
2026年IEEE TASE,面对突发危险区域的基于强化学习的多无人机路径规划,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
AI科技星2 小时前
全维度相对论推导、光速螺旋时空与北斗 GEO 钟差的统一理论
开发语言·线性代数·算法·机器学习·数学建模
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 279. 完全平方数 | C++ 动态规划 (完全背包)
c++·leetcode·动态规划
ECT-OS-JiuHuaShan2 小时前
科学的本来意义,是基于规范的共识逻辑,而非共识方法
人工智能·科技·学习·算法·生活
木子墨5162 小时前
LeetCode 热题 100 精讲 | 动态规划进阶篇:最大子数组和 · 分割等和子集 · 最长公共子序列 · 打家劫舍 III
数据结构·c++·算法·leetcode·动态规划·力扣
li1670902702 小时前
第十章:list
c语言·开发语言·数据结构·c++·算法·list·visual studio
Z1Jxxx2 小时前
C++ P1150 Peter 的烟
数据结构·c++·算法
踮起脚看烟花2 小时前
chapter10_泛型算法
c++·算法