leetcode- Preorder/Inorder/PostOrder without Recursive

Recursive solution is trivial

方法一(麻烦):

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

     vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        if(root==NULL) return result;
        stack<TreeNode*> visit;
        TreeNode* p=root;
        visit.push(p);
        result.push_back(p->val);
        while(visit.size()>0){            
            while(p->left){
                p=p->left;
                visit.push(p);
                result.push_back(p->val);
            }
            TreeNode* tmp=visit.top();
            visit.pop();
            while(!tmp->right && visit.size()>0){
                tmp=visit.top();
                visit.pop();
            }
            if(visit.size()==0&& !tmp->right) // the root has a left son only
                return result; 
            p=tmp->right;
            visit.push(p);
            result.push_back(p->val);
        }
        return result;
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        if(root==NULL) return result;
        stack<TreeNode*> visit;
        TreeNode* p=root;
        visit.push(p);
        while(visit.size()>0){            
            while(p->left){
                p=p->left;
                visit.push(p);
            }
            TreeNode* tmp=visit.top();
            result.push_back(tmp->val);
            visit.pop();
            while(!tmp->right && visit.size()>0){
                tmp=visit.top();
                result.push_back(tmp->val);
                visit.pop();
            }
            if(visit.size()==0&& !tmp->right) // the root has  a left son only
                return result; 
            p=tmp->right;
            visit.push(p);
        }
        return result;
    }

    //后序遍历,稍微麻烦一些,因为要区分从左子树和从右子树回溯的状态,所以加上了一个状态
    // 下面的求公共最小祖先用到后序遍历
     void transform(vector<pair<TreeNode*,bool>> pairs,vector<TreeNode*> &path){
        for(int i=0;i<pairs.size();i++)
            path.push_back(pairs[i].first);
    }
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root==NULL) return NULL;
        vector<pair<TreeNode*, bool>> stack1;
        vector<TreeNode*> path_p,path_q;
        TreeNode *sp=root;// searchp 
        while(sp!=NULL|| stack1.size()>0){
            while(sp!=NULL){
                stack1.push_back(make_pair(sp,true));  // judge every node : p or q ? after push a new node.
                if(sp==p) transform(stack1,path_p);//path_p=stack; //  when find , save the stackte(path) 
                if(sp==q) transform(stack1,path_q);
                if(path_p.size()>0 && path_q.size()>0 && path_p.back()==p && path_q.back()==q)  break; 
                sp=sp->left;
            }
            if(stack1.size()>0){
                sp=stack1.back().first;
                bool flag=stack1.back().second;
                if(flag){ // the first time on the top of stack1
                    stack1.back().second=false;
                    sp=sp->right;
                }else{// the second time on the top of stack1, pop it from stack1
                    stack1.pop_back();
                    sp=NULL;
                }
            }
        }
        int length=(path_p.size()<path_q.size())?path_p.size():path_q.size();
        for(int i=0;i<length;i++)
            if(path_p[i]!=path_q[i])
                return path_p[i-1];
         return path_p[length-1];       
    }

方法二(更简单):

 vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        if(root==NULL) return result;

        stack<pair<TreeNode*,bool>> pairs;
        pairs.push(make_pair(root,false));
        bool visited;
        while(!pairs.empty()){

            TreeNode *tmp=pairs.top().first;
            visited=pairs.top().second;
            pairs.pop();
            if(tmp==NULL) continue;
            if(visited){
                result.push_back(tmp->val);
            }else{
                pairs.push(make_pair(tmp,true));//(1)
                pairs.push(make_pair(tmp->right,false));//(2)
                pairs.push(make_pair(tmp->left,false));//(3)
            }
        }
        return result;
    }

前序和中序遍历只需要调整(1),(2),(3)处的代码,用到的基本原理是:局部有序,且每个相邻的局部有重合,则整体有序

参考:
http://zisong.me/post/suan-fa/geng-jian-dan-de-bian-li-er-cha-shu-de-fang-fa

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值