struct node
{
int data;
node* left;
node* right;
int tag = 1;
}
void reNode(node* root)
{
if(root == NULL)
return;
//cout << root->data << endl; //递归前序遍历
reNode(root->left);
//cout << root->data << endl; //递归中序遍历
reNode(root->right);
//cout << root->data << endl; //递归后序遍历
}
void teNode(node* root)
{
node* p = root;
stack<node*> s;
while(p != NULL || !s.empty())
{
while(p != NULL)
{
s.push_back(p);
//cout << p->data << endl; //前序遍历
p = p->left;
}
if(!s.empty())
{
p = s.top();
s.pop();
//cout << p->data << endl; // 中序遍历
p = p->right();
}
}
}
void posNode(node* root)
{
stack<node*> s;
node* p = root;
while(p != NULL || !s.empty())
{
while(!p)
{
s.push_back(p);
p = p->left;
}
if(!s.empty())
{
p = s.top();
if(p->tag)
{
cout << p->data << endl; // 后序遍历
s.pop();
p = NULL;
}
else
{
p->tag = 1;
p = p->right;
}
}
}
}