//这个demo是测试树的建立,遍历(递归和非递归的前中后序遍历已经层次遍历)
//调用栈
using namespace std;
vector<int> prelist;
vector<int> inlist;
vector<int> postlist;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
int findA(vector<int> res,int val){
for(int i=0;i<res.size();i++){
if(res[i]==val)
return i;
}
return -1;
}
void Test(TreeNode* &root,vector<int>& pre,vector<int>& ino,int lp,int rp,int li,int ri){
if(lp>rp||li>ri)
return ;
root = new TreeNode(pre[lp]);
int pos = findA(ino,pre[lp]);
Test(root->left,pre,ino,lp+1,lp+pos-li,li,pos-1);
Test(root->right,pre,ino,lp+pos-li+1,rp,pos+1,ri);
}
TreeNode* buildTree(vector<int>& pre,vector<int>& in){
if(pre.size()<0)
return NULL;
TreeNode* root = NULL;
Test(root,pre,in,0,pre.size()-1,0,in.size()-1);
return root;
}
//下面这三块是递归遍历
void preOrder(TreeNode* root){
if(root!=NULL){
prelist.push_back(root->val);
preOrder(root->left);
preOrder(root->right);
}
}
void inOrder(TreeNode* root){
if(root!=NULL){
inOrder(root->left);
inlist.push_back(root->val);
inOrder(root->right);
}
}
void postOrder(TreeNode* root){
if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
postlist.push_back(root->val);
}
}
//下面三块是非递归操作(非递归是要借助栈来实现的)
vector<int> NoRecursionInPreOrder(TreeNode* root){
vector<int> res;
if(root==NULL)
return res;
stack<TreeNode*> s;
TreeNode* p = root;
while(!s.empty()||p){
while(p){
res.push_back(p->val);
s.push(p);
p = p->left;
}
p = s.top();
s.pop();
p = p->right;
}
return res;
}
vector<int> NoRecursionInInOrder(TreeNode* root){
vector<int> res;
if(root==NULL)
return res;
stack<TreeNode*> s;
TreeNode* p = root;
while(!s.empty()||p){
while(p){
s.push(p);
p = p->left;
}
p = s.top();
res.push_back(p->val);
s.pop();
p = p->right;
}
return res;
}
vector<int> NoRecursionInPostOrder(TreeNode* root){
vector<int> res;
if(root==NULL)
return res;
stack<TreeNode*> s;
TreeNode* p = NULL;
while(!s.empty()||root!=NULL){
while(root!=NULL){
s.push(root);
root = root->left;
}
root = s.top();
s.pop();
if(root->right==NULL||root->right==p){
res.push_back(root->val);
p = root;
root = NULL;
}
else{
s.push(root);
root = root->right;
}
}
return res;
}
//下面这个是层次遍历
vector<vector<int>> levelOrder(TreeNode* root){
vector<vector<int>> res;
if(root==NULL)
return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n = q.size();
vector<int> t;
for(int i=0;i<n;i++){
TreeNode* f = q.front();
t.push_back(f->val);
q.pop();
if(f->left)
q.push(f->left);
if(f->right)
q.push(f->right);
}
res.push_back(t);
}
return res;
}
int main(){
vector<int> pre{3,9,20,15,7};
vector<int> ino{9,3,15,20,7};
TreeNode* root = NULL;
root = buildTree(pre,ino);//用先序和中序建树
//普通建树(暴力建树)
TreeNode* root1 = new TreeNode(3);
root1->left = new TreeNode(9);
root1->right = new TreeNode(20);
TreeNode* test = root1;
test = test->right;
test->left = new TreeNode(15);
// test->right = new TreeNode(7);
preOrder(root);
inOrder(root);
postOrder(root);
cout<<"递归:"<<endl;
cout<<"先序:";
for(auto c:prelist)
cout<<c<<" ";
cout<<endl;
cout<<"中序:";
for(auto c:inlist)
cout<<c<<" ";
cout<<endl;
cout<<"后序:";
for(auto c:postlist)
cout<<c<<" ";
cout<<endl;
cout<<"非递归:"<<endl;
cout<<"先序:";
for(auto c:NoRecursionInPreOrder(root))
cout<<c<<" ";
cout<<endl;
cout<<"中序:";
for(auto c:NoRecursionInInOrder(root))
cout<<c<<" ";
cout<<endl;
cout<<"后序:";
for(auto c:NoRecursionInPostOrder(root))
cout<<c<<" ";
cout<<endl;
cout<<"层次遍历:"<<endl;
vector<vector<int>> res = levelOrder(root1);
for(int i=0;i<res.size();i++){
for(int j=0;j<res[i].size();j++){
cout<<res[i][j]<<" ";
}
cout<<endl;
}
return 0;
}