//二叉树的层次遍历
#include<vector>
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 300;
char s[maxn];//保存读入的值
bool failed;//标志变量
struct Node
{
bool have_value;//是否被赋值过
int v;//结点值
Node *Left;
Node *Right;
Node():have_value(false),Left(NULL),Right(NULL){}//构造函数
};
Node *root;//根结点
Node *newnode(){return new Node();}//申请空间并执行构造函数
void addnode(int v,char *s)
{
int n = strlen(s);
Node *u = root;
for (int i = 0;i<n;i++)
{
if(s[i]=='L')
{
if(u->Left==NULL)//若节点不存在 建立新节点
u->Left=newnode();
u = u->Left; //往左走
}
else if(s[i]=='R')
{
if(u->Right==NULL)
u->Right=newnode();
u = u->Right;
}
}//忽略最后的括号
if(u->have_value)//已经被赋过值
{
failed = true;
//cout<<"*"<<endl;
}
u->v = v;
u->have_value = true;//做标记
}
bool read_inpute()
{
failed = false;
root = newnode();//建立根节点
for(;;)
{
if(scanf("%s",s)!=1) return false;
if(strcmp(s,"()")==0) break;
int v;
sscanf(&s[1],"%d",&v);
addnode(v,strchr(s,',')+1);//strchr函数返回s中从左往右第一个字符“,”的指针
}
return true;
}
bool bfs(vector<int> &ans)
{
queue<Node*> q;
ans.clear();
q.push(root);
while(!q.empty())
{
Node *u = q.front();
q.pop();
if(!u->have_value)//路径上有的节点只分配了内存空间而没有赋值
failed = true;
ans.push_back(u->v);
if(u->Left!=NULL) q.push(u->Left);
if(u->Right!=NULL) q.push(u->Right);
}
return true;
}
int main()
{
vector<int>ans;
vector<int>::iterator it;
while(read_inpute())
{
if(failed == true||root->have_value==false)
{
cout<<"not complete"<<endl;
continue;
}
bfs(ans);
for(it=ans.begin();it!=ans.end();it++)
{
if(it==ans.begin())
cout<<*it;
else
cout<<" "<<*it;
}
cout<<endl;
}
return 0;
}
#include<vector>
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 300;
char s[maxn];//保存读入的值
bool failed;//标志变量
struct Node
{
bool have_value;//是否被赋值过
int v;//结点值
Node *Left;
Node *Right;
Node():have_value(false),Left(NULL),Right(NULL){}//构造函数
};
Node *root;//根结点
Node *newnode(){return new Node();}//申请空间并执行构造函数
void addnode(int v,char *s)
{
int n = strlen(s);
Node *u = root;
for (int i = 0;i<n;i++)
{
if(s[i]=='L')
{
if(u->Left==NULL)//若节点不存在 建立新节点
u->Left=newnode();
u = u->Left; //往左走
}
else if(s[i]=='R')
{
if(u->Right==NULL)
u->Right=newnode();
u = u->Right;
}
}//忽略最后的括号
if(u->have_value)//已经被赋过值
{
failed = true;
//cout<<"*"<<endl;
}
u->v = v;
u->have_value = true;//做标记
}
bool read_inpute()
{
failed = false;
root = newnode();//建立根节点
for(;;)
{
if(scanf("%s",s)!=1) return false;
if(strcmp(s,"()")==0) break;
int v;
sscanf(&s[1],"%d",&v);
addnode(v,strchr(s,',')+1);//strchr函数返回s中从左往右第一个字符“,”的指针
}
return true;
}
bool bfs(vector<int> &ans)
{
queue<Node*> q;
ans.clear();
q.push(root);
while(!q.empty())
{
Node *u = q.front();
q.pop();
if(!u->have_value)//路径上有的节点只分配了内存空间而没有赋值
failed = true;
ans.push_back(u->v);
if(u->Left!=NULL) q.push(u->Left);
if(u->Right!=NULL) q.push(u->Right);
}
return true;
}
int main()
{
vector<int>ans;
vector<int>::iterator it;
while(read_inpute())
{
if(failed == true||root->have_value==false)
{
cout<<"not complete"<<endl;
continue;
}
bfs(ans);
for(it=ans.begin();it!=ans.end();it++)
{
if(it==ans.begin())
cout<<*it;
else
cout<<" "<<*it;
}
cout<<endl;
}
return 0;
}