二叉树的前中后序遍历 C++ 非递归

三种遍历的思路都是从跟开始遍历,将树分为根和左数,用栈存起来,根据栈的特性,改变访问根的时机

前序:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) 
    {
        vector<int> ret;  
        stack<TreeNode*>st;

        TreeNode*cur=root;
        while(cur||!st.empty())
        {
            while(cur)
            {
                ret.push_back(cur->val);
                st.push(cur);
                cur=cur->left;
            }

            TreeNode*top=st.top();
            cur=top->right;
            st.pop();
        }

        return ret;

    }
};

中序:

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) 
    {
        vector<int> ret;
        stack<TreeNode*>st;

        TreeNode*cur=root;
        while(cur||!st.empty())
        {

            while(cur)
            {
                st.push(cur);
                cur=cur->left;
            }
            
            TreeNode*top=st.top();
            ret.push_back(top->val);
            cur=top->right;
            st.pop();
        }
        return ret;
    }
};

后序:

(后序要麻烦一些,因为根是最后一个被记录,所以需要记录右数是否被记录)

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

        return ret;
    
    }
};

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是二叉树后序遍历C++ 代码: ```c++ #include <iostream> #include <stack> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) return; cout << root->val << " "; preorderTraversal(root->left); preorderTraversal(root->right); } // 序遍历 void inorderTraversal(TreeNode* root) { if (root == NULL) return; inorderTraversal(root->left); cout << root->val << " "; inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(TreeNode* root) { if (root == NULL) return; postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } // 非递归序遍历 void preorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* curr = s.top(); s.pop(); cout << curr->val << " "; if (curr->right != NULL) s.push(curr->right); if (curr->left != NULL) s.push(curr->left); } } // 非递归序遍历 void inorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s; TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); cout << curr->val << " "; curr = curr->right; } } // 非递归后序遍历 void postorderTraversalNonRecursive(TreeNode* root) { if (root == NULL) return; stack<TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { TreeNode* curr = s1.top(); s1.pop(); s2.push(curr); if (curr->left != NULL) s1.push(curr->left); if (curr->right != NULL) s1.push(curr->right); } while (!s2.empty()) { cout << s2.top()->val << " "; s2.pop(); } } int main() { TreeNode* root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); cout << "Preorder traversal: "; preorderTraversal(root); cout << endl; cout << "Inorder traversal: "; inorderTraversal(root); cout << endl; cout << "Postorder traversal: "; postorderTraversal(root); cout << endl; cout << "Non-recursive preorder traversal: "; preorderTraversalNonRecursive(root); cout << endl; cout << "Non-recursive inorder traversal: "; inorderTraversalNonRecursive(root); cout << endl; cout << "Non-recursive postorder traversal: "; postorderTraversalNonRecursive(root); cout << endl; return 0; } ``` 输出结果为: ``` Preorder traversal: 1 2 4 5 3 6 7 Inorder traversal: 4 2 5 1 6 3 7 Postorder traversal: 4 5 2 6 7 3 1 Non-recursive preorder traversal: 1 2 4 5 3 6 7 Non-recursive inorder traversal: 4 2 5 1 6 3 7 Non-recursive postorder traversal: 4 5 2 6 7 3 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值