九度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;
}*/
相关文章推荐
- 九度OJ,题目1009-二叉搜索树
- Android数据存储实现的5大方式
- 从线行回归到高斯过程分类
- HttpURLConnection请求数据
- tornado study 1
- 面试题五 从头到尾打印单链表
- Java学习之集合框架(中)
- Linux下的文件及文件后缀名
- 高并发的简单处理方法
- 模板类错误程序
- 面向对象多继承
- [leetcode] 【字符串】 67. Add Binary
- Spring MVC 获取请求参数的几种方法
- ADB Commands Cheat Sheet
- Xamarin开发安装Visual Studio 2015 update2报错的解决办法
- VUEJS实战之利用laypage插件实现分页(3)
- Java或Web中解决所有路径问题
- leetcode 299. Bulls and Cows
- Mybatis学习与Spring MVC 的集成(6)
- kylin mondrian saiku interaction