mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 1900.cpp
This commit is contained in:
parent
912dbb8f98
commit
ffbffff91e
91
QUSTOJ/1900.cpp
Normal file
91
QUSTOJ/1900.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <queue>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct btree
|
||||||
|
{
|
||||||
|
btree* l;
|
||||||
|
btree* r;
|
||||||
|
char v;
|
||||||
|
};
|
||||||
|
|
||||||
|
btree _node;
|
||||||
|
btree* root=&_node;
|
||||||
|
|
||||||
|
void deal(btree* n)
|
||||||
|
{
|
||||||
|
scanf("%c",&n->v);
|
||||||
|
if(n->v=='#')
|
||||||
|
{
|
||||||
|
n->l=n->r=NULL;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n->l=new btree;
|
||||||
|
deal(n->l);
|
||||||
|
n->r=new btree;
|
||||||
|
deal(n->r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void NLR(btree* n)
|
||||||
|
{
|
||||||
|
if(n->v!='#')
|
||||||
|
{
|
||||||
|
printf("%c ",n->v);
|
||||||
|
if(n->l) NLR(n->l);
|
||||||
|
if(n->r) NLR(n->r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void LNR(btree* n)
|
||||||
|
{
|
||||||
|
if(n->v!='#')
|
||||||
|
{
|
||||||
|
if(n->l) LNR(n->l);
|
||||||
|
printf("%c ",n->v);
|
||||||
|
if(n->r) LNR(n->r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LRN(btree* n)
|
||||||
|
{
|
||||||
|
if(n->v!='#')
|
||||||
|
{
|
||||||
|
if(n->l) LRN(n->l);
|
||||||
|
if(n->r) LRN(n->r);
|
||||||
|
printf("%c ",n->v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LEVEL(btree* n)
|
||||||
|
{
|
||||||
|
queue<btree*> bus;
|
||||||
|
bus.push(n);
|
||||||
|
|
||||||
|
while(!bus.empty())
|
||||||
|
{
|
||||||
|
btree* p=bus.front();
|
||||||
|
bus.pop();
|
||||||
|
if(p->v!='#') printf("%c ",p->v);
|
||||||
|
if(p->l) bus.push(p->l);
|
||||||
|
if(p->r) bus.push(p->r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
deal(root);
|
||||||
|
printf("Preorder:");
|
||||||
|
NLR(root);
|
||||||
|
printf("\nInorder:");
|
||||||
|
LNR(root);
|
||||||
|
printf("\nPostorder:");
|
||||||
|
LRN(root);
|
||||||
|
printf("\nLevelorder:");
|
||||||
|
LEVEL(root);
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user