zoukankan      html  css  js  c++  java
  • 树的遍历非递归实现

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <map>
    #include <vector>
    #include <set>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    
    using namespace std;
    
    struct Node
    {
        int val;
        Node* left;
        Node* right;
        Node(int value)
        {
            this->val = value;
            this->left=NULL;
            this->right=NULL;
        }
    };
    
    void preOrder(Node* root)
    {
        if(root==NULL)
            return;
        cout<<root->val;
        preOrder(root->left);
        preOrder(root->right);
    }
    
    void inOrder(Node* root)
    {
        if(root==NULL)
            return;
    
        inOrder(root->left);
        cout<<root->val;
        inOrder(root->right);
    }
    
    void postOrder(Node* root)
    {
        if(root==NULL)
            return;
    
        postOrder(root->left);
    
        postOrder(root->right);
        cout<<root->val;
    
    }
    void buildTree(Node* &tree)
    {
        tree = new Node(4);
        tree->left = new Node(3);
        tree->left->left = new Node(5);
        //tree->left->left->right = new Node(10);
        //tree->left->left->right->left = new Node(11);
        tree->left->right = new Node(6);
        tree->right = new Node(7);
        tree->right->left = new Node(8);
        tree->right->right = new Node(9);
    }
    
    int main()
    {
    
        Node* tree;
        buildTree(tree);
        preOrder(tree);
        cout<<endl;
        stack<Node*> s;
        s.push(tree);
    
        while(!s.empty())
        {
            Node* temp = s.top();
            s.pop();
    
            cout<<temp->val;
            if(temp->right!=NULL)
                s.push(temp->right);
            if(temp->left!=NULL)
                s.push(temp->left);
        }
    
        cout<<endl;
    
        Node* tree2=tree;
        cout << tree->val;
        s.push(tree);
    
        while(!s.empty()) {
    
            Node *temp = s.top();
    
            if (temp->left != NULL) {
                cout << temp->left->val;
                s.push(temp->left);
                temp->left=NULL;
                continue;
            }
    
            s.pop();
            if (temp->right != NULL) {
                cout << temp->right->val;
                s.push(temp->right);
                temp->right=NULL;
                continue;
            }
    
    
        }
        cout<<endl;
    
        buildTree(tree);
        Node* temp = tree;
        while(!s.empty()||temp!=NULL)
        {
            if(temp!=NULL)
            {
                cout<<temp->val;
                s.push(temp);
                temp=temp->left;
            }
            else
            {
                temp = s.top();
                s.pop();
                temp = temp->right;
            }
        }
        cout<<endl;
    
        inOrder(tree);
    
        cout<<endl;
    
        s.push(tree);
    
        while(!s.empty())
        {
            Node* temp = s.top();
            if(temp->left!=NULL)
            {
                s.push(temp->left);
                temp->left=NULL;
                continue;
            }
            cout<<temp->val;
            s.pop();
            if(temp->right!=NULL)
            {
                s.push(temp->right);
                temp->right=NULL;
                continue;
            }
        }
    
        cout<<endl;
        buildTree(tree);
        temp = tree;
        while(!s.empty()||temp!=NULL)
        {
            if(temp!=NULL)
            {
                s.push(temp);
                temp=temp->left;
            } else{
                temp = s.top();
                cout<<temp->val;
                s.pop();
                temp = temp->right;
            }
        }
        cout<<endl;
        buildTree(tree);
    
        postOrder(tree);
        cout<<endl;
        s.push(tree);
    
        while(!s.empty())
        {
            Node* temp = s.top();
            if(temp->left!=NULL)
            {
                s.push(temp->left);
                temp->left=NULL;
                continue;
            }
            if(temp->right!=NULL)
            {
                s.push(temp->right);
                temp->right=NULL;
                continue;
            }
            s.pop();
            cout<<temp->val;
        }
    
        cout<<endl;
        buildTree(tree);
    
        s.push(tree);
    
        vector<int> ans;
    
        while(!s.empty())
        {
            Node* temp = s.top();
            s.pop();
    
            ans.push_back(temp->val);
            if(temp->left!=NULL)
                s.push(temp->left);
            if(temp->right!=NULL)
                s.push(temp->right);
        }
    
        for(int i=ans.size()-1;i>=0;i--)
            cout<<ans[i];
    
        return 0;
    }
    
  • 相关阅读:
    Kubernetes中的Service Mesh(第5部分):Dogfood环境和入口
    A Service Mesh for Kubernetes(第1部分): Service的重要指标
    A Kubernetes in Service Mesh(第9部分):使用gRPC的乐趣和收益
    Automated, Self-Service Provisioning of VMs Using HyperForm (Part 2) (使用HyperForm自动配置虚拟机(第2部分)
    Authentication in Loopback Applications Against Bluemix(在针对Bluemix的Lookback应用中进行身份认证)
    java工具类-excel jxl
    南阳199
    南阳198
    南阳168
    南阳274
  • 原文地址:https://www.cnblogs.com/dacc123/p/12570669.html
Copyright © 2011-2022 走看看