zoukankan      html  css  js  c++  java
  • 二叉树递归和非递归

    /*
    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;
            
        }
    };
  • 相关阅读:
    284. Peeking Iterator 光是看看下一个值的遍历
    339. Nested List Weight Sum 339.嵌套列表权重总和
    341. Flatten Nested List Iterator展开多层数组
    springcloud之配置中心服务化和高可用
    springcloud之配置中心git
    springcloud之熔断监控Hystrix Dashboard和Turbine
    springcloud之熔断器Hystrix
    springcloud之服务提供与调用
    springcloud之注册中心Eureka
    springcloud之大话springcloud
  • 原文地址:https://www.cnblogs.com/yuguangyuan/p/13387397.html
Copyright © 2011-2022 走看看