您的位置:首页 > 其它

九度OJ,题目1009-二叉搜索树

2016-06-13 11:01 573 查看
 

题目1009:二叉搜索树

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:7537

解决:3335

题目描述:
判断两序列是否为同一二叉搜索树序列
输入:
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。

接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。

接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
输出:
如果序列相同则输出YES,否则输出NO

样例输入:
2
567432
543267
576342
0

样例输出:
YES
NO


1、建立二叉搜索树

2、一个一个比较判断是否相同,有两种判断度方法,一种是比较前序遍历(例如print函数打出来的),一种是比较每棵树的左子树和右子树,如果不同就判断不同

一遍A的我好激动啊~~~~~

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct tree
{
tree *lc;
tree *rc;
int num;
};
int n;
char s[10005];
char p[1005][10005];
int sz[10005];
int len1,len2;
const int INF = 1000005;
void insert(int sz,tree *rt)
{
if(sz > (rt->num))//insert node greater than current node
{
if(rt->rc==NULL)
{
tree *root = new tree;
root->num = sz;
root->lc=NULL;
root->rc=NULL;
rt->rc = root;
return;
}
else//insert node less than current node
{
insert(sz,rt->rc);
return;
}
return;
}
else
{
if(rt->lc==NULL)
{
tree *root = new tree;
root->lc=NULL;
root->rc=NULL;
root->num = sz;
rt->lc = root;
return;
}
else
{
insert(sz,rt->lc);
return;
}
return;
}
return;
}
void print(tree *r)
{
cout<<r->num<<endl;
if(r->lc!=NULL)
{
print(r->lc);
}
if(r->rc!=NULL)
{
print(r->rc);
}
return;
}
int flag;
void cmp(tree* a, tree *b)
{
if(a->lc!=NULL&&b->lc!=NULL)
{
if(a->lc->num == b->lc->num)
cmp(a->lc,b->lc);
else flag =1;
}
else if(a->rc!=NULL&&b->rc!=NULL)
{
if(a->rc->num == b->rc->num)
cmp(a->rc,b->rc);
else flag =1;
}
else if(a->rc!=NULL&&b->rc==NULL||a->rc==NULL&&b->rc!=NULL)
{
flag = 1;
}
}
int main()
{
while(cin>>n&&n)
{
scanf("%s",s);
int ans= 0;
len1 = strlen(s);
//cout<<len1<<" ";
for(int i = 0 ; i < len1 ;i++)
{
sz[i] = s[i]-'0';
}
tree *ro = new tree;
ro->num = sz[0];
ro->lc =NULL;
ro->rc = NULL;
for(int i = 1 ; i < len1 ;i++)
{
insert(sz[i],ro);
}
// print(ro);
for(int i = 0 ;i < n; i++)
{
scanf("%s",p[i]);
}

for(int i = 0 ; i < n ;i++)
{
flag = 0;
len2 =strlen(p[i]);
tree *roc = new tree;
roc->num = p[i][0]-'0';
roc->lc =NULL;
roc->rc = NULL;
for(int j = 1 ; j < len2;j++)
{
insert(p[i][j]-'0',roc);
}
cmp(ro,roc);
if(flag==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
}
/*
测试数据
2
567432
543267
576342
YES NO
*/
/*
*
* 失败代码尝试
*/
/*
int findgreater(int c,int *sz,int len)
{

for(int i = 0 ; i< len ;i++)
{
if(sz[i]>c)
return i;
}
return -1;
}
int findless(int c,int *sz ,int len)
{
for(int i = 0 ; i < len ;i++)
{
if(sz[i]<c)
return i;
}
return -1;
}*/
/*tree* build(int *sz,int len)
{
tree* root = new tree;
root->rc = NULL;
root->lc = NULL;
int c = sz[0];
root->num = c;
if(sz[0]==-INF||len<=1)
return root;
if(sz[1]<sz[0])
{
int wzg = findgreater(c,sz,len);
cout<<"wzg"<<wzg;
root->lc = build(sz+1,wzg-1);
if(wzg>=0)
root->rc = build(sz+wzg,len-wzg);
else
root->rc=NULL;
}
else
{
int wzl = findless(c,sz,len);
cout<<"wzl"<<wzl;
root->rc = build(sz+1,wzl-1);
if(wzl>=0)
root->lc = build(sz+wzl,len-wzl);
}
return root;
}*/



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: