代码随想录算法训练营第14天|二叉树的前序、中序、后序遍历【递归法、迭代法】

本文详细介绍了二叉树的前序、后序和中序遍历的递归实现以及迭代法,通过代码展示了如何使用栈来辅助遍历过程。
摘要由CSDN通过智能技术生成

144. 二叉树的前序遍历

Problem: 144. 二叉树的前序遍历

前序:中左右!

Code

递归方法

/**
 * 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 {
    vector<int> res;
public:
    vector<int> preorderTraversal(TreeNode* root) {
        preTraver(root);
        return res;
    }

    void preTraver(TreeNode* root){
        if(root==nullptr){//递归边界
            return;
        }
        res.push_back(root->val);//前序遍历(先操作当前节点)
        preTraver(root->left);
        preTraver(root->right);
    }
    
};

迭代法

/**
 * 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) return res;
        st.push(root);
        while(!st.empty()){
            //入栈:跟节点->右孩子->左孩子
            //每次访问,先出现一个节点(在压入这个节点的右孩子和左孩子),这样在下次访问下一个节点的时候,先访问的会是左孩子
            TreeNode* cur = st.top();
            st.pop();
            int num = cur->val;
            res.push_back(num);
            if(cur->right!=nullptr) st.push(cur->right);
            if(cur->left!=nullptr) st.push(cur->left);
        }
        return res;
    }
};

145. 二叉树的后序遍历

Problem: 145. 二叉树的后序遍历

左右中

Code

递归方法

/**
 * 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 {
    vector<int> res;
public:
    vector<int> postorderTraversal(TreeNode* root) {
        postTravel(root);
        return res;
    }
    //左右中
    void postTravel(TreeNode* cur){
        if(cur == nullptr) return;
        postTravel(cur->left);
        postTravel(cur->right);
        res.push_back(cur->val);
    }

};

迭代法

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

二叉树的中序遍历

Problem: 94. 二叉树的中序遍历

左中右

Code

递归法

/**
 * 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 {
    vector<int> res;
public:
    vector<int> inorderTraversal(TreeNode* root) {
        inorder(root);
        return res;
    }

    void inorder(TreeNode* cur){
        if(cur == nullptr) return;
        inorder(cur->left);
        res.push_back(cur->val);
        inorder(cur->right);
    }
    
};

迭代法:中序遍历的迭代法不是很好理解,但是思想还是左右中
栈做辅助

/**
 * 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;
        if(root==nullptr) return res;
        stack<TreeNode*> st;
        //左中右(左子树),只要有左孩子,就一直压入栈
        TreeNode* cur = root;
        while(!st.empty() || cur != nullptr){
            //这里依旧是左中右的访问顺序,左的操作是,只要左子树非空,就左孩子
            //左子树为空,就需要访问当前的元素(中),在压入右子树
            if(cur!=nullptr){
                st.push(cur);
                cur = cur->left;
            }else{
                cur = st.top();
                st.pop();
                res.push_back(cur->val);
                cur = cur->right;
            }
        }
    return res;
    }
};
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值