LeetCode 二叉树遍历 递归 & 迭代 栈+空指针

二叉树的递归遍历

二叉树的前中后遍历其实就是中间节点的遍历顺序。
递归三要素:

  1. 确定递归函数的参数与返回值
//无需返回值
//需要知道当前节点,以及一个用于保存遍历结果的vector数组
void traversal(TreeNode* cur, vector<int>& vec)
  1. 确定终止条件
if (cur == NULL) return;
  1. 确定单层递归的处理逻辑
//以前序遍历为例
vec.push_back(cur->val);    // 中
traversal(cur->left, vec);  // 左
traversal(cur->right, vec); // 右

前序遍历

题目链接

class Solution {
public:
    void traversal(TreeNode* cur, vector<int>& res){
        if(cur == nullptr) return;
    
        res.push_back(cur->val);
        traversal(cur->left, res);
        traversal(cur->right, res);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root, res);
        return res;
    }
};

中序遍历

题目链接

class Solution {
public:
    void traversal(TreeNode* cur, vector<int>& res){
        if(cur == nullptr) return;
        traversal(cur->left, res);
        res.push_back(cur->val);
        traversal(cur->right, res);
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root, res);
        return res;
    }
};

后序遍历

题目链接

class Solution {
public:
        void traversal(TreeNode* cur, vector<int>& res){
        if(cur == nullptr) return;
        
        traversal(cur->left, res);
        traversal(cur->right, res);
        res.push_back(cur->val);
    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root, res);
        return res;
    }
};

二叉树的迭代遍历 用栈模拟+使用空指针来统一风格

前序遍历

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){
            TreeNode* cur = st.top();
            st.pop();
            if(cur != nullptr){
                if(cur->right!=nullptr) st.push(cur->right); // 右
                if(cur->left!=nullptr) st.push(cur->left); // 左
                st.push(cur); // 中
                st.push(nullptr); //空节点表明轮到中节点
            }
            else{
                cur = st.top();
                st.pop();
                res.push_back(cur->val);
            }
        } 
        return res;
    }
};

中序遍历

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        //迭代
        stack<TreeNode*> st;
        vector<int> res;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){//栈非空 
            //中序遍历 左中右
            TreeNode* cur = st.top();
            st.pop();
            if(cur!=nullptr){
                if(cur->right!=nullptr) st.push(cur->right);
                
                st.push(cur);
                st.push(nullptr);
                
                if(cur->left!=nullptr) st.push(cur->left);
            }
            else{//下一个节点为中节点
                cur = st.top();
                st.pop();
                res.push_back(cur->val);
            }
        }
        return res;
    }
};

后序遍历

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        stack<TreeNode*> st;
        if(root!=nullptr) st.push(root);
        while(!st.empty()){
            TreeNode* cur = st.top();
            st.pop();
            if(cur != nullptr){
                st.push(cur);
                st.push(nullptr);
                if(cur->right!=nullptr) st.push(cur->right);
                if(cur->left!=nullptr) st.push(cur->left);

            }
            else{
                cur = st.top();
                st.pop();
                res.push_back(cur->val);
            }
        } 
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值