头文件
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
前序遍历
void preorder1(bintree t)
{
vector<bintree >s;
bintree p=t;
while(p!=NULL||!s.empty()){
while(p!=NULL){
cout<<p->data<<" ";
s.push_back(p);
p=p->lchild;
}
if(!s.empty()){
p=s.back();
s.pop_back();
p=p->rchild;
}
}
}
中序遍历
void inorder1(bintree t)
{
vector<bintree >s;
bintree p=t;
while(p!=NULL||!s.empty()){
while(p!=NULL){
s.push_back(p);
p=p->lchild;
}
if(!s.empty()){
p=s.back();
cout<<p->data<<" ";
s.pop_back();
p=p->rchild;
}
}
}
后序遍历
void postorder1(bintree t)
{
vector<bintree >s;
vector<char> v;
bintree p=t;
while(p!=NULL||!s.empty()){
while(p!=NULL){
v.push_back(p->data);
s.push_back(p);
p=p->rchild;
}
if(!s.empty()){
p=s.back();
s.pop_back();
p=p->lchild;
}
}
reverse(v.begin(), v.end());
for(int i=0;i<v.size();i++){
cout<<v[i]<<' ';
}
}