二叉搜索树

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

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
 
Source
 
Recommend
notonlysuccess   |   We have carefully selected several
similar problems for you:  3787 3790 3789 3788 1710 
 
我的思路:
建一棵树,上面放有flag,当有其他数组近来匹配时,若经过的节点的flag为0则,不相等,否则,将与该数相等的树的节点的flag设为1。
 
 
可以这样建树
struct tree                //声明树的结构
{
struct tree *left;
int data;
struct tree *right;
};

本题代码

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
typedef struct treenode *tree;//定义treenode这个结构体是一个指针
struct treenode
{
int v;
tree left, right;//这个结构体指针又含有两个指针
bool f;//用于标记该节点曾经有没有经过
};
using namespace std; tree newnode(int x)//新建节点
{
tree t = (tree)malloc(sizeof(treenode));
t->v = x;
t->left = t->right = NULL;
t->f = ;
return t;//要有返回值
} tree insert(tree t,int x)//插入节点
{
if (!t) t = newnode(x);//如果节点为空,建一个节点
else
{
if (x < t->v)
{
t->left = insert(t->left, x);//不能写成t=。。。因为是插入在左边
}
else
t->right = insert(t->right, x);
}
return t;//要有返回值
} bool check(tree t, int x)
{
if (!t) return ;//x在树t中没有出现过
else
{
if (t->v == x)//相等,标记经过该节点
{
t->f = ;
return ;
}
else
{
if (t->f == ) return ;//经过了一个曾经没经过的节点
else
{
if (x < t->v)
return check(t->left, x);
else
return check(t->right, x);
}
}
}
} void init(tree t)//初始化树上的f
{
if (t)
{
t->f = ;
init(t->left);
init(t->right);
}
} int main()
{
int n;
char a[];
while (cin >> n && n)
{
tree t = (tree)malloc(sizeof(treenode));//开辟空间建第一个节点
cin >> a;
int l = strlen(a);
t->v = a[]-'';
t->left = t->right = NULL;
int i;
for (i = ; i < l; i++)
{
int x = a[i] - '';
t = insert(t,x);//插入节点;
}
while (n--)
{
cin >> a;
bool f = ;
for (i = ; i < l; i++)
{
int x = a[i] - '';
f = check(t, x);//进入树t检查
if (!f) break;
}
if (!f) cout << "NO" << endl;
else cout << "YES" << endl;
init(t);//把树上的f重新设为0
}
free(t);//释放树
}
return ;
}

HDU 3179 二叉搜索树(树的建立)的更多相关文章

  1. hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)

    二叉搜索树 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  2. HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...

  3. hdu 3791 二叉搜索树(数据结构)

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

  4. HDU 3791 二叉搜索树

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

  5. HDU 3791 二叉搜索树 题解

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

  6. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  7. 【剑指Offer】26、二叉搜索树与双向链表

      题目描述:   输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.   解题思路:   首先要理解此题目的含义,在双向链表中,每个结 ...

  8. 原生JS实现二叉搜索树(Binary Search Tree)

    1.简述 二叉搜索树树(Binary Search Tree),它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子 ...

  9. LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

随机推荐

  1. mygene 3.0.0

    MyGene.Info provides simple-to-use REST web services to query/retrieve gene annotation data. It’s de ...

  2. django 常用字段类型

    <> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数据库层和Django校验层限制该字段所允许的最大字符数 ...

  3. python 列表字符串元素乱序

    from random import shuffle color = ['] shuffle(color) print(color)

  4. Builder(建造者)

    意图: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性: 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时. 当构造过程必须允许被构造的对象有不同 ...

  5. DB.使用Oracle时的遇到的问题

    1.(20190225)ojdbc14.jar 来自“E:\ZC_DB\_Connector\Oracle\10g\Oracle Database 10g Release 2 (10.2.0.4) J ...

  6. 解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"错误

    今天安装了Android Studio 3.2,打开一个旧工程,编译提示"No toolchains found in the NDK toolchains folder for ABI w ...

  7. PHP自定义XML类实现数组到XML文件的转换

    这两天在公司写和各应用商店应用内搜索的接口,大致就像百度应用内搜索这样的东西,具体可以点下面的链接查看. 百度应用内搜索 有的应用商店需要JSON格式的数据,所以我只需要用下面的语句就可以返回对方服务 ...

  8. python学习笔记(自定义库文件路径)

    博主最近在弄接口自动化.主要是基于python自带的unittest框架.包括 Pubilc模块定义所有接口. Main模块根据业务需求重新封装接口便于测试. config文件导入测试业务的固定参数. ...

  9. 利用JS将页面中包含“一个”字段值的元素改为红色

    document.body.innerHTML = document.body.innerHTML.replace(/一个/ig,"<span style='color: red;'& ...

  10. Python 实现图片上表格的写入

    直接上代码:import matplotlib.pylab as pltimport numpy as npplt.figure()axes=plt.gca()y= np.random.randn(9 ...