【数据结构】二叉树的非递归遍历(前序、中序、后序)

二叉树的非递归写法比较难理解,经常忘,所以粗略写一下。

二叉树的非递归前序遍历

前序遍历的非递归写法比较容易理解。
前序遍历的顺序是“根左右”,因此一旦碰到一个结点,我们管它有没有孩子,先把它的值读下来,然后注意,先入栈的是右孩子,后入栈的是左孩子。为什么呢?因为栈的特点是先进后出,我们需要先访问左孩子,所以左孩子后入栈。最好画个图自己模拟一下出栈入栈的过程,有助于理解。

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

二叉树的非递归中序遍历

中序遍历的遍历顺序是“左根右”,也就是说,需要先把左子树访问完,再访问自己,最后访问右子树。所有先一直往左走,走到没有左孩子的结点 X 时,出栈,访问 X,然后往右走,访问右子树的结点…想一想,自己在求中序遍历时的过程是怎么样的,最好画图模拟一下这个过程。

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        TreeNode *p = root;
        
        while (!st.empty() || p != nullptr) {
            if (p) {
                st.push(p);
                p = p->left;
            } else {
                p = st.top();
                st.pop();
                res.push_back(p->val);
                p = p->right;
            }
        }
        return res;
    }
};

二叉树的非递归后序遍历

有两种写法

第一种写法是双栈写法,是“假”的后序遍历。为什么这么说呢?这种写法借助了前序遍历的思想 。前序遍历的顺序是“根左右”,而后序遍历的顺序是“左右根”。前序遍历的时候,我们是先把右孩子入栈,再把左孩子入栈,而如果先把左孩子入栈,后把右孩子入栈,就变成了“根右左”,最后再借助另一个栈反转顺序就得到了后序遍历的结果。可以对比前序遍历的代码来理解。

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(!root) return res;
        stack<TreeNode*> st;
        st.push(root);
        while (!st.empty()) {
            TreeNode *t = st.top();
            st.pop();
            res.push_back(t->val);
            if (t->left) st.push(t->left);
            if (t->right) st.push(t->right);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

第二种写法是单栈写法比较难理解,但是真正还原了后序遍历的过程
后序遍历的顺序是“左右根”,即把孩子全部遍历完再遍历自己。所以要先一直往左走,到达左边的结点 X 后,此时还要检查 X 有没有右孩子,如果有右孩子 XR,还要往右走,再检查 XR 有没有左孩子,若有,一直往左走…
栈中保存的是还没有遍历完孩子的结点,也都是栈顶结点的所有祖先节点,一旦发现栈顶结点的孩子遍历完,就出栈

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode*> st;
        TreeNode *p = root, *r = nullptr;

        while (p != nullptr || !st.empty()) {
            if (p != nullptr) {
                st.push(p);
                p = p->left;
            } else {
                p = st.top();
                if (p->right != nullptr && p->right != r) {
                    p = p->right;
                } else {
                    st.pop();
                    ans.push_back(p->val);
                    r = p;
                    p = nullptr;
                }
            }
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++数据结构中树的二叉树前序中序后序遍历的递归与非递归代码,包括输入与输出。 假设二叉树的节点定义如下: ``` struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(NULL), right(NULL) {} }; ``` 前序遍历: 递归版本: ``` void preorderTraversal(Node* root) { if (!root) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } ``` 非递归版本: ``` void preorderTraversal(Node* root) { stack<Node*> s; s.push(root); while (!s.empty()) { Node* node = s.top(); s.pop(); if (!node) continue; cout << node->val << " "; s.push(node->right); s.push(node->left); } } ``` 中序遍历: 递归版本: ``` void inorderTraversal(Node* root) { if (!root) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } ``` 非递归版本: ``` void inorderTraversal(Node* root) { stack<Node*> s; Node* node = root; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 后序遍历: 递归版本: ``` void postorderTraversal(Node* root) { if (!root) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } ``` 非递归版本: ``` void postorderTraversal(Node* root) { stack<Node*> s; Node* node = root; Node* last = NULL; while (node || !s.empty()) { while (node) { s.push(node); node = node->left; } node = s.top(); if (!node->right || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = NULL; } else { node = node->right; } } } ``` 输入与输出: ``` int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); cout << "前序遍历(递归): "; preorderTraversal(root); cout << endl; cout << "前序遍历(非递归): "; preorderTraversal(root); cout << endl; cout << "中序遍历(递归): "; inorderTraversal(root); cout << endl; cout << "中序遍历(非递归): "; inorderTraversal(root); cout << endl; cout << "后序遍历(递归): "; postorderTraversal(root); cout << endl; cout << "后序遍历(非递归): "; postorderTraversal(root); cout << endl; return 0; } ``` 输出结果: ``` 前序遍历(递归): 1 2 4 5 3 6 7 前序遍历(非递归): 1 2 4 5 3 6 7 中序遍历(递归): 4 2 5 1 6 3 7 中序遍历(非递归): 4 2 5 1 6 3 7 后序遍历(递归): 4 5 2 6 7 3 1 后序遍历(非递归): 4 5 2 6 7 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值