数据结构实验之二叉树七:叶子问题
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
Input
输入数据有多行,每一行是一个长度小于
50
个字符的字符串。
Output
按从上到下从左到右的顺序输出二叉树的叶子结点。
Example Input
abd,,eg,,,cf,,, xnl,,i,,u,,
Example Output
dfg uli
#include <iostream>
#include <string.h>
#include <queue>
#include <stack>
#include <malloc.h>
using namespace std;
typedef struct btree
{
char data;
btree *lchild,*rchild;
}btree;
int i;
char ch[100];
btree *root;
btree * createbtree(btree *&root)
{
char c;
c=ch[i++];
if(c==',')
root=NULL;
else
{
root=(btree *)malloc(sizeof(btree));
root->data=c;
root->lchild=createbtree(root->lchild);
root->rchild=createbtree(root->rchild);
}
return root;
}
void levelorder()
{
btree *b=root;
queue<btree*>Queue;
Queue.push(b);
while(!Queue.empty())
{
b=Queue.front();
Queue.pop();//这个没有返回值
if(b->lchild==NULL&&b->rchild==NULL)
cout<<b->data;
if(b->lchild!=NULL)
Queue.push(b->lchild);
if(b->rchild!=NULL)
Queue.push(b->rchild);
}
cout<<endl;
}
int main()
{
while(cin>>ch)
{
i=0;
root=createbtree(root);
levelorder();
}
return 0;
}
#include <iostream>
#include <string.h>
#include <queue>
#include <stack>
#include <malloc.h>
using namespace std;
typedef struct btree
{
char data;
btree *lchild,*rchild;
}btree;
int i;
char ch[100];
btree *root;
btree * createbtree(btree *&root)
{
char c;
c=ch[i++];
if(c==',')
root=NULL;
else
{
root=(btree *)malloc(sizeof(btree));
root->data=c;
root->lchild=createbtree(root->lchild);
root->rchild=createbtree(root->rchild);
}
return root;
}
void levelorder()
{
btree *b=root;
queue<btree*>Queue;
Queue.push(b);
while(!Queue.empty())
{
b=Queue.front();
Queue.pop();//这个没有返回值
if(b->lchild==NULL&&b->rchild==NULL)
cout<<b->data;
if(b->lchild!=NULL)
Queue.push(b->lchild);
if(b->rchild!=NULL)
Queue.push(b->rchild);
}
cout<<endl;
}
int main()
{
while(cin>>ch)
{
i=0;
root=createbtree(root);
levelorder();
}
return 0;
}
数据结构实验之二叉树五:层序遍历
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立二叉树并求二叉树的层次遍历序列。
Input
输入数据有多行,第一行是一个整数t (t<1000),代表有t行测试数据。每行是
一个长度小于50个字符的字符串。
Output
输出二叉树的层次遍历序列。
Example Input
2 abd,,eg,,,cf,,, xnl,,i,,u,,
Example Output
abcdefg xnuli
#include <iostream>
#include <queue>
#include <malloc.h>
using namespace std;
typedef struct btree
{
char data;
btree *lchild,*rchild;
}btree;
char ch[55];
int i;
void createbtree(btree *&root)
{
char c=ch[i++];
if(c==',')
root=NULL;
else//按先序建立二叉树,要是按照中序后序的顺序自己应该会会写出
{
root=(btree *)malloc(sizeof(btree));
root->data=c;
createbtree(root->lchild);
createbtree(root->rchild);
}
}
void levelorder(btree *&root)
{
queue<btree*>Queue;
btree *p=root;
Queue.push(root);
while(!Queue.empty())
{
p=Queue.front();
cout<<p->data;
Queue.pop();
if(p->lchild!=NULL)
Queue.push(p->lchild);
if(p->rchild!=NULL)
Queue.push(p->rchild);
}
}
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
i=0;
cin>>ch;
btree *root;
root=(btree*)malloc(sizeof(btree));
createbtree(root);
levelorder(root);
cout<<endl;
}
}
return 0;
}
/***************************************************
User name: YT1658506207邵雪源
Result: Runtime Error
Take time: 0ms
Take Memory: 0KB
Submit time: 2017-11-06 18:31:30
****************************************************/
#include <iostream>
#include <queue>
#include <malloc.h>
using namespace std;
typedef struct btree
{
char data;
btree *lchild,*rchild;
}btree;
char ch[55];
int i;
void createbtree(btree *&root)
{
char c=ch[i++];
if(c==',')
root=NULL;
else//按先序建立二叉树,要是按照中序后序的顺序自己应该会会写出
{
root=(btree *)malloc(sizeof(btree));
root->data=c;
createbtree(root->lchild);
createbtree(root->rchild);
}
}
void levelorder(btree *&root)
{
queue<btree*>Queue;
btree *p=root;
Queue.push(root);
while(!Queue.empty())
{
p=Queue.front();
cout<<p->data;
Queue.pop();
if(p->lchild!=NULL)
Queue.push(p->lchild);
if(p->rchild!=NULL)
Queue.push(p->rchild);
}
}
int main()
{
int n;
while(cin>>n)
{
while(n--)
{
i=0;
cin>>ch;
btree *root;
root=(btree*)malloc(sizeof(btree));
createbtree(root);
levelorder(root);
cout<<endl;
}
}
return 0;
}
/***************************************************
User name: YT1658506207邵雪源
Result: Runtime Error
Take time: 0ms
Take Memory: 0KB
Submit time: 2017-11-06 18:31:30
****************************************************/