关闭

leetcode- Preorder/Inorder/PostOrder without Recursive

标签: postorderpreorderinorder非递归算法
74人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26310次
    • 积分:590
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条