二叉搜索树(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. 这道 ...
随机推荐
- UWP 多语言的三个概念
首先了解一下 RFC4646 和 BCP-47 是什么东西: RFC4646 The name is a combination of an ISO 639 two-letter lowercase ...
- Spring Framework 4.3.22.RELEASE Reference文档目录
<Spring Framework Reference Documentation 4.3.22.RELEASE> https://docs.spring.io/spring/docs/4 ...
- Bash/Shell-脚本整理(长期更新)
轮询检测Apache状态并启用钉钉报警 #!/bin/bash shell_user="root" shell_domain="apache" shell_li ...
- LOJ#3048. 「十二省联考 2019」异或粽子(trie树+堆)
题面 传送门 题解 我们先把它给前缀异或和一下,然后就是要求前\(k\)大的\(a_i\oplus a_j\).把\(k\)乘上个\(2\),变成前\(2k\)大的\(a_i\oplus a_j\), ...
- Java并发编程总结5——ThreadPoolExecutor
一.ThreadPoolExecutor介绍 在jdk1.8中,构造函数有4个.以 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, ...
- C#6.0语言规范(十三) 接口
接口定义合同.实现接口的类或结构必须遵守其合同.接口可以从多个基接口继承,并且类或结构可以实现多个接口. 接口可以包含方法,属性,事件和索引器.接口本身不为它定义的成员提供实现.接口仅指定必须由实现接 ...
- 用.NET WebService Studio调试Web Service解决SOAPAction的问题
话说是这样的,这两天开发一个短信发送功能,客户给了一个 Web Service 地址(没有文档),让我调用就可以发送了, 我在VS 2013添加了服务引用,一切正常,可是执行代理方法时,怎么都报错 R ...
- Git 本地操作
版权声明:数学是研究世界的本质,自然科学是研究上帝的意志,而计算机则是揣摩屌丝人类的意志 目录(?)[-] 命令 git config 增删改查 init clone add commit sta ...
- iOS-贝塞尔连续曲线
一个曲线 UIColor *color = [UIColor redColor]; [color set]; UIBezierPath *path = [UIBezierPath bezierPath ...
- centos 安装nginx笔记
添加nginx 存储库 yum install epel-release 安装 yum install nginx 启动 systemctl start nginx