# 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;
}

#### 面试过程中手撕代码之二叉树

2017-08-31 11:20:55

#### 算法：C++实现二叉树遍历（递归、非递归）

2017-07-03 22:01:58

#### 一个简单的二叉树实现

2016-08-11 15:24:09

#### C++建立二叉排序树

2017-03-08 13:53:12

#### [LeetCode]513. Find Bottom Left Tree Value

2017-07-20 22:06:53

#### 二叉树-preorder+inorder 与& postorder+inorder

2014-08-11 11:30:54

#### 二叉树前序Preorder遍历和后序Postorder遍历的非递归实现

2014-06-09 20:59:21

#### LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

2013-11-12 13:20:50

#### Given preorder and inorder traversal of a tree, construct the binary tree.

2014-12-04 23:07:11

#### Binary Tree Traversal(Preorder, Inorder, Postorder )

2016-09-19 10:41:03