题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3791

题意:给定一个n(多组,n为0时结束),给一个串和n个串,分别判断n个串按序列构建的二叉搜索树和第一给串相同。分别转换成先序判断。

#include<bits/stdc++.h>
using namespace std;
struct node
{
int v;
node *left,*right;
}*root;
node* build(node *root,int v)
{
if(root==NULL)
{
root=new node();
root->v=v;
root->left=root->right=NULL;
}
else if(v<=root->v)root->left=build(root->left,v);
else root->right=build(root->right,v);
return root;
} string ans1,ans2,s;
void dfs(node *root)
{
ans1+=to_string(root->v);
if(root->left!=NULL)dfs(root->left);
if(root->right!=NULL)dfs(root->right);
}
int main()
{
int n;
while(cin>>n&&n)
{
ans1="";
root=NULL;
cin>>s;
for(int i=;s[i];i++)root=build(root,s[i]-'');
dfs(root);
ans2=ans1; while(n--)
{
ans1="";
root=NULL;
cin>>s;
for(int i=;s[i];i++)root=build(root,s[i]-'');
dfs(root); if(ans1==ans2)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return ;
}

hdu3791二叉搜索树的更多相关文章

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

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

  2. 二叉搜索树(hdu3791)

    二叉搜索树 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  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. egret清除缓存的方法

    1 图片加版本号2 js加版本号3 default.res.json 加版本号 3个缺一不可 ps: egret 里面default.res.json 资源和代码一定要保持一致

  2. Linux 部署Nginx反向代理服务 使用openssl自生成证书并配置https

    1.安装Nginx编译所依赖的包 正常centos中可以使用yum安装一下依赖包: yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...

  3. 安装&卸载Windows服务

    使用.NET Framework的工具InstallUtil.exe. 安装服务 C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.e ...

  4. Thrift总结(四)Thrift实现双向通信

    前面介绍过 Thrift 安装和使用,介绍了Thrift服务的发布和客户端调用,可以查看我之前的文章:https://www.cnblogs.com/zhangweizhong/category/10 ...

  5. nyoj 399-整除个数 (整除)

    399-整除个数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:9 提交数:18 难度:1 题目描述: 1.2.3… …n这n(0<n<=1000000000)个数中有 ...

  6. DAL

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;u ...

  7. Python数据强制类型转换

    本文链接:https://www.cnblogs.com/zyuanlbj/p/11909992.html 常用转换函数 函数 作用 int(x) 将x转换成整数类型 float(x) 将 x 转换成 ...

  8. ArcGIS API For Javascript :如何制作地图切换器

    大部分情况下我们开发会使用原生的地图切换器,由于每个项目的页面风格不同,业务场景不同,因此需要做一些样式不同的地图切换器. 首先可以照猫画虎,自己照着地图切换器的样式抄一个,或者看看主流的地图切换器都 ...

  9. 工作日志,error parsing query: unable to find time zone

    工作日志,error parsing query: unable to find time zone 坑 Windows 系统使用influxdb数据库,在执行查询语句时提示 ERR: error p ...

  10. Unix, Linux以及NT内核和它们各自衍生的系统关系图