#ifndef __TREE_H
#define __TREE_H
#include <iostream>
#include <stack>
using namespace std;
struct BTree{
int data;
BTree* left;
BTree* right;
};
void Pre_Order(BTree* rt)
{
if(rt == NULL)
return ;
BTree* pNode = rt;
stack<BTree*> S;
while(!S.empty() || pNode != NULL)
{
while(pNode)
{
cout << pNode->data << " ";
S.push(pNode);
pNode = pNode->left;
}
if(!S.empty())
{
pNode = S.top();
S.pop();
pNode = pNode->right;
}
}
return ;
}
void In_Order(BTree* rt)
{
if(rt == NULL)
return ;
BTree* pNode = rt;
stack<BTree*> S;
while(!S.empty() || pNode != NULL)
{
while(pNode)
{
S.push(pNode);
pNode = pNode->left;
}
if(!S.empty())
{
pNode = S.top();
cout << pNode->data << " ";
S.p
小程序(3)——二叉树遍历的非递归实现(三种遍历顺序)
最新推荐文章于 2023-10-29 16:36:41 发布