leetcode- Preorder/Inorder/PostOrder without Recursive

原创 2016年08月30日 14:14:53

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

相关文章推荐

LeetCode Week9: Binary Tree Preorder/InOrder/PostOrder Traversal

这一周完成的题目主要是Tree部分的题目,这里选择3道经典的题来编写题解,分别是Binary Tree Preorder Traversal、Binary Tree Inorder Traversal...

【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A...

leetcode Binary Tree Preorder Traversal Binary Tree Postorder Traversal Inorder Traversal

最近几天都没有在leetcode做题了,今天来了做了几个二叉树。发现二叉树的题目真的很多,都是些一般的基础。没有什么说的。找两个最近的题做了贴上吧。 前序遍历二叉树,这个没有什么说的。借助于堆栈可...

leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal

其实这两道题目就是从二叉树的前序遍历(后序遍历)和中序遍历数组中恢复出二叉数即可。我们知道二叉树的三种遍历方法后本题就变得很简单了。 先序遍历: 若二叉树为空,则不进行任何操作...

[Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)

题目大意根据二叉树的前序遍历和中序遍历( 中序和后序)结果生成二叉树 假设没有重复数字解题思路参考给中序和后序遍历 看到树首先想到要用递归来解题。以这道题为例:如果一颗二叉树为{1,2,3,4,5...

LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal

1. 题目描述 105 Given preorder and inorder traversal of a tree, construct the binary tree. 106 Give...

leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal

Given preorder and inorder (Inorder and Postorder) traversal of a tree, construct the binary tree. ...

(Leetcode)利用preorder-inorder/postorder-inorder构建二叉树

105. Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversa...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode- Preorder/Inorder/PostOrder without Recursive
举报原因:
原因补充:

(最多只允许输入30个字)