二叉搜索树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2280    Accepted Submission(s): 994

Problem Description
判断两序列是否为同一二叉搜索树序列
 
Input
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。 接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。 接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
 
Output
如果序列相同则输出YES,否则输出NO
 
Sample Input
2
567432
543267
576342
0
 
Sample Output
YES
NO
 
思路很简单,建树,然后输出比较一下就好了,
对像我这种刚刚入门的还是有难度啊,,,,,
#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)的更多相关文章

  1. HDU3791二叉搜索树(二叉树)

    Problem Description 判断两序列是否为同一二叉搜索树序列   Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束.接下去一行是一 ...

  2. hdu3791二叉搜索树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3791 题意:给定一个n(多组,n为0时结束),给一个串和n个串,分别判断n个串按序列构建的二叉搜索树和 ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  5. [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 ...

  6. [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 ...

  7. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. [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 ...

  9. [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. 这道 ...

随机推荐

  1. CF1146H Satanic Panic

    题目传送门 Description 给定二维平面内\(n\)个点\((n\leq 300)\),求能组成五角星(不要求正五角星)的五元组个数. Solution 一道小清新的寄蒜几盒计算几何题,代码不 ...

  2. 【Java初探01】——Java简介及相关

    Java 简介 java 是一种高级的面向对象的程序设计语言,使用Java语言编写的程序时跨平台的.从pc到手机,都有Java开发的程序和游戏,Java程序可以在任何计算机,操作系统和支持的Java的 ...

  3. maven封装jar包遇到的问题

    使用eclipse编译后可以生成jar包,使用mvn clean package指令打包报错,错误如下:No compiler is provided in this environment. Per ...

  4. Python FTP文件传输

    FTP Server import socket import struct from concurrent.futures import ThreadPoolExecutor import json ...

  5. Celery启动Django项目:Client sent AUTH, but no password is set 错误处理

    celery -A CeleryTest worker -l info [2017-02-22 07:26:52,666: ERROR/MainProcess] consumer: Cannot co ...

  6. (转)9 db2trc案例2(1,2)

    原文:http://book.51cto.com/art/200906/130068.htm 9.3.3  db2trc案例2(1) 在AIX操作系统上,系统原先运行良好,而后用户从DB2 V8 FP ...

  7. android开发学习——day6

    关于UI的几个插件学习 button和textview,以及点击button利用Toast提醒,editText private EditText editText; @Override protec ...

  8. 二、LINQ之查询表达式基础

    1.查询是什么? 查询是一组指令,描述要从给定数据源(或源)检索的数据以及返回的数据应具有的形状和组织.查询表达式和它所产生的结果不同.

  9. 全网最详细的Windows里下载与安装Sublime Text *(图文详解)

    不多说,直接上干货! 前言 这是代码编程软件,可以性感编程自己代码,有着非常丰富的插件,界面整洁清爽.第一次使用可能不习惯,当你使用一段时间之后,你就会爱上它. 下载与安装 1.下载:目前官方的正式版 ...

  10. C#基础篇三流程控制1

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace P01R ...