二叉搜索树(hdu3791)
二叉搜索树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2280 Accepted Submission(s): 994
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; typedef struct tree
{
tree *right,*left;
int num;
}tree;
tree *root; int a[],b[],count=; tree *creat(int x)//建树
{
tree *t=(tree *)malloc(sizeof(tree));
t->right=NULL;
t->left=NULL;
t->num=x;
return t;
} tree *inster(tree *s,int x)//插入
{
tree *t;
if(s==NULL)
{
t=creat(x);
s=t;
}
else
{
if(x<=s->num)
s->left=inster(s->left,x);
else
s->right=inster(s->right,x);
}
return s;
}
void libian(tree *root)
{
if(root!=NULL)
{
b[count++]=root->num;
libian(root->left);
libian(root->right);
}
}
int main()
{
int n;
while(scanf("%d",&n)>&&n)
{
count=;
root=NULL;
char str[];
scanf("%s",str);
int len=strlen(str);
int i,j;
for(i=;i<len;i++)
{
int tmp=str[i]-;
root=inster(root,tmp);
}
libian(root);
for(i=;i<len;i++)
a[i]=b[i];
while(n--)
{
count=i=;
scanf("%s",str);
root=NULL;
for(i=;i<len;i++)
{
int tmp=str[i]-;
root=inster(root,tmp);
}
libian(root);
for(i=;i<len;i++)
if(a[i]!=b[i])
{
printf("NO\n");
break;
}
if(i>=len)
printf("YES\n");
}
}
return ;
}
前几天放假,玩了几天,接下来继续学。。。。努力!
二叉搜索树(hdu3791)的更多相关文章
- HDU3791二叉搜索树(二叉树)
Problem Description 判断两序列是否为同一二叉搜索树序列 Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束.接下去一行是一 ...
- hdu3791二叉搜索树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3791 题意:给定一个n(多组,n为0时结束),给一个串和n个串,分别判断n个串按序列构建的二叉搜索树和 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- PS插件CameraRaw-初次尝试
一.百度百科原话 RAW的原意就是“未经加工”.可以理解为:RAW图像就是CMOS或者CCD图像感应器将捕捉到的光源信号转化为数字信号的原始数据.RAW文件是一种记录了数码相机传感器的原始信息,同时记 ...
- 2019/4/23 todolist
近期的任务单子大概是这样吧 bjoi2019改完,写题解 hnoi2019改一些,写题解 找3道网络流写写 写一场agc,写题解 找2道简单计算几何写写
- springBoot的搭建使用记录
一: 首次搭建:https://blog.csdn.net/u013187139/article/details/68944972 整合mybatis: https://www.jianshu.com ...
- VS2013 编辑器
1. VS -> 本地Git -> Github 1. 右键单击解决方案,选择“将解决方案添加到源代码管理器”,选择Git 2. 切换到团队资源管理器([菜单]视图->团队资源管理器 ...
- 【sping揭秘】7、国际化信息支持
Spring提供messagesource接口,来进行国际化事务处理 Applicationcontext会优先找一个名为messageSouce的messageSource接口实现bean,如果找不 ...
- POJ 2551
#include<iostream> #include<stdio.h> #include<string> using namespace std; //int m ...
- 【原创】关于程序卸载的一个Bug
今天解决了一个问题,程序安装目录下的某个文件不能被卸载,干净环境下不能重现,某些计算机可以重现. 解决: 这个问题里有两个文件不能被卸载 1.由程序生成的文件,如日志,即不是通过安装包安装的文件在卸载 ...
- shell 终端terminfo命令 tput
tput命令 tput 可以更改终端功能,如移动或更改光标,更改文本属性,清除终端屏幕的特定区域等. 光标属性 在shell脚本或命令行中,可以利用tput命令改变光标属性. tput clear # ...
- (转载)elasticsearch 查询(match和term)
原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...
- 多线程编程——java
1.进程和线程 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种资源和状态信息,包括打开的文件.子进程和信号 ...