二叉树递归和非递归

 

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/

class TreeToSequence {
public:
    
       void  pre(TreeNode* root,vector<int>&a){

           if(root== NULL){
               return ;
           }
           a.push_back(root->val);
           pre(root->left,a);
           pre(root->right,a);
       }

   void mid(TreeNode* root,vector<int>&a){
        if(root== NULL){
            return ;
        }
        mid(root->left,a);
        a.push_back(root->val);
        mid(root->right,a);
    }
    void  aft(TreeNode* root,vector<int>&a){
        if(root== NULL){
            return ;
        }
        aft(root->left,a);
        aft(root->right,a); 
        a.push_back(root->val);

    }
    
    vector<vector<int> > convert(TreeNode* root) {
        // write code here
        vector<vector<int> >a;
        vector<int> p;
        vector<int> m;
        vector<int> af;
        
        pre(root,p);
        mid(root,m);
        aft(root,af);
        a.push_back(p);
        a.push_back(m);
        a.push_back(af);
        return a;
    }
};

 

后续遍历  left ->right ->root;

可以看成root->right->left 的逆序。 因此会前序遍历,后续遍历就是前序的 思想,加逆序数组。

 

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/

class TreeToSequence {
public:
    
    
    void pre(TreeNode* root,vector<int> &a){
        
        stack<TreeNode*>s;
        TreeNode* n = root;
        
        while(n||!s.empty()){

            while(n){
                a.push_back(n->val);
                s.push(n);
                n = n->left;
            }
            if(!s.empty()){
                TreeNode* tmp = s.top();
                s.pop();
                n = tmp->right;
            }
        }
        
        /*
        stack<TreeNode*> B;
        TreeNode* temp=root;
        B.push(temp);
        while(!B.empty()){
            temp=B.top();
            B.pop();
            A.push_back(temp->val);
            if(temp->right)
                B.push(temp->right);
            if(temp->left)
                B.push(temp->left);
        }
        */
    }
    void mid( TreeNode* root,vector<int> &a){
        
        stack<TreeNode*>s;
        TreeNode* n = root;
        
        while(n || !s.empty()){

            while(n){
                s.push(n);
                n = n->left;

            }
            if(!s.empty()){
                TreeNode* v = s.top();

                a.push_back(v->val);
                s.pop();
                n = v->right;
            }
        }
/*
        stack<TreeNode*> B;
        TreeNode* temp=root;
        while(!B.empty()||temp!=nullptr){
            while(temp!=nullptr) {
                B.push(temp);
                temp=temp->left;
            }
            if(!B.empty()){
                temp=B.top();
                A.push_back(temp->val);
                B.pop();
                temp=temp->right;
            }
        }
        */
    }
    void aft( TreeNode* root,vector<int> &a){
    /*
        stack<TreeNode*> B;
        stack<TreeNode*> C;
        TreeNode* temp=root;
        B.push(root);
        while(!B.empty()){
            temp=B.top();
            B.pop();
            C.push(temp);
            if(temp->left!=nullptr)
                B.push(temp->left);
            if(temp->right!=nullptr)
                B.push(temp->right);
        }
        while(!C.empty()){
            temp=C.top();
            C.pop();
            A.push_back(temp->val);
        } 
        */
        TreeNode* n = root;
        stack<TreeNode*>s;
        while(n || !s.empty()){

            while(n){
                a.push_back(n->val);
                s.push(n);
                n = n->right;
              
            }
            if(!s.empty()){
                TreeNode* v = s.top();
                s.pop();
                n = v->left;
            }
        }
        reverse(a.begin(),a.end());
        

    
        
    }

    
    
    
    vector<vector<int> > convert(TreeNode* root) {
        // write code here
        vector<vector<int> > ret;
        vector<int> p;
        vector<int> m;
        vector<int> a;

        pre( root,p);
        ret.push_back(p);
        
        mid( root,m);
        ret.push_back(m);

        aft( root,a);
        ret.push_back(a);
        
        return ret;
        
    }
};

 

posted @ 2020-07-27 20:16  于光远  阅读(217)  评论(0编辑  收藏  举报