#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#define MAXSIZE 20
typedef struct bnode
{
char data;
struct bnode *lchild,*rchild;
}BNode,*bitree;
bitree pre_create()//创建二叉树
{
bitree bt;
char ch;
ch=getchar();
if(ch==' ')
return NULL;
else
{
bt=new BNode[sizeof(BNode)];
bt->data=ch;
bt->lchild=pre_create();
bt->rchild=pre_create();
}
}
void pre_order(bitree bt)//非递归先序遍历
{
stack<bitree> s;
bitree p=bt;
while(p||!s.empty())
{
while(p)
{
s.push(p);
cout<<p->data<<" ";
p=p->lchild;
}
if(!s.empty())
{
二叉树的先序、中序、后序的非递归遍历(分别用两个栈和一个带标志位的栈两种方法去做)和层次遍历
最新推荐文章于 2021-03-01 22:22:28 发布
本文详细介绍了如何使用两个栈和一个带有标志位的栈实现二叉树的非递归先序、中序、后序遍历,并探讨了层次遍历的方法。通过实例解析,帮助读者深入理解这些遍历策略的实现过程。
摘要由CSDN通过智能技术生成