hdu3791二叉搜索树
题目链接: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二叉搜索树的更多相关文章
- HDU3791二叉搜索树(二叉树)
Problem Description 判断两序列是否为同一二叉搜索树序列 Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束.接下去一行是一 ...
- 二叉搜索树(hdu3791)
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- [数据结构]——二叉树(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. 这道 ...
随机推荐
- activemq 的延迟队列和幂等性检查
一. 延迟消息队列 1. 在提交支付之后,可以发送一个延迟检查的队列,来主动查询用户在支付宝上的支付状态 在mq的配置/config/activeMq.xml的broker实例上配置 schedule ...
- java笔试面试第一天
好久未曾启用我的博客,最近来上海找工作,想将笔试面试的过程做个记录,毕竟有总结才有提高嘛.今天算是笔试面试正式开始第一天吧,以下就是我的笔试总结(没有原题了,只有知识点): 笔试题1:java sta ...
- 关于@Autowired 与@Resource的
@Autowired注解是spring自己定义的,@Resource是由JSR-250规范定义的注解. @Resource的作用相当于@Autowired,只不过@Autowired按byType自动 ...
- Arduino 处理JSON格式的数据
Arduino 处理JSON格式的数据 1.安装 ArduinoJson这个包 2.程序代码 # include <ArduinoJson.h> #define ALINK_BODY_FO ...
- nyoj 266-字符串逆序输出 (isdigit(), geline(cin, my_string))
266-字符串逆序输出 内存限制:64MB 时间限制:3000ms 特判: No 通过数:15 提交数:18 难度:0 题目描述: 给定一行字符,逆序输出此行(空格.数字不输出) 输入描述: 第一行是 ...
- vim编辑中断后,重新编辑的警告删除
使用vim 编辑,遇到突然中断,比如ssh远程时断网了. 如果再次ssh连接,重新vim 打开之前在编辑的文件,会有类似如下的警告 这是因为vim会在被编辑的目录下新建一个名为.filename.sw ...
- <automate the boring stuff with python>---第七章 正则实例&正则贪心&匹配电话号码和邮箱
第七章先通过字符串查找电话号码,比较了是否使用正则表达式程序的差异,明显正则写法更为简洁.易扩展.模式:3 个数字,一个短横线,3个数字,一个短横线,再是4 个数字.例如:415-555-4242 i ...
- 2019-9-28:渗透测试,phpstudy后门,利用复现
9月20号爆出Phpstudy存在隐藏后门,简单复现下后门效果 该文章仅供学习,利用方法来自网络文章,仅供参考 目标机:win7系统,安装phpstudy 2018版,php版本5.2或php 5.4 ...
- 用Helm3构建多层微服务
Helm是一款非常流行的k8s包管理工具.以前就一直想用它,但看到它产生的文件比k8s要复杂许多,就一直犹豫,不知道它的好处能不能抵消掉它的复杂度.但如果不用,而是用Kubectl来进行调式真的很麻烦 ...
- sql注入问题回顾
(以下语法均为在python中使用mysql语句,部分代码省略,使用python中的pymsql模块获取游标对象即可直接执行sql语句) sql注入:在传入参数的时候做出改变,使得插入数据这条sql语 ...