HDU 3179 二叉搜索树(树的建立)
二叉搜索树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6293 Accepted Submission(s):
2820
的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
567432
543267
576342
0
NO
similar problems for you: 3787 3790 3789 3788 1710
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 二叉搜索树(树的建立)的更多相关文章
- hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
二叉搜索树 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...
- hdu 3791 二叉搜索树(数据结构)
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 3791 二叉搜索树
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 3791 二叉搜索树 题解
Problem Description 推断两序列是否为同一二叉搜索树序列 Input 開始一个数n,(1<=n<=20) 表示有n个须要推断,n= 0 的时候输入结束. 接下去一行是 ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- 【剑指Offer】26、二叉搜索树与双向链表
题目描述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 解题思路: 首先要理解此题目的含义,在双向链表中,每个结 ...
- 原生JS实现二叉搜索树(Binary Search Tree)
1.简述 二叉搜索树树(Binary Search Tree),它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子 ...
- 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 ...
随机推荐
- 第四章 consul cluster
1.vagrant 为了模拟集群效果,使用vagrant. 1.1.首先下载vagrant https://www.vagrantup.com/downloads.html 说明:浏览器下载可能比较慢 ...
- How to Restore “TrustedInstaller” as Default Owner of a File
type NT SERVICE\TrustedInstaller in "Enter the object name to select" text box http://www. ...
- Java实现时钟小程序【代码】
哎,好久没上博客园发东西了,上一次还是两个月前的五一写的一篇计算器博客,不过意外的是那个程序成了这学期的Java大作业,所以后来稍微改了一下那个程序就交了上去,这还是美滋滋.然后五月中旬的时候写了一个 ...
- 雷林鹏分享:Ruby XML, XSLT 和 XPath 教程
Ruby XML, XSLT 和 XPath 教程 什么是 XML ? XML 指可扩展标记语言(eXtensible Markup Language). 可扩展标记语言,标准通用标记语言的子集,一种 ...
- git add 的一点说明
git add --cached 这里 --cached是什么意思呢?要解释清楚这个问题,我们必须先了解一个文件在git中的状态. [commit]----[stage]-----[checkout] ...
- 手把手教你如何加入到github的开源世界
我曾经一直想加入到开源项目中,但是因为没有人指导流程,网上看了很多,基本都是说了个大概,如果你也是一个初出茅庐的人,那么,我将以自己提交的一次开源代码为例,教会你步入开源的世界. 1,首先登陆到htt ...
- cf812 C 二分
C. Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Linux终端界面屏保
Linux终端界面屏保 在很多Linux使用者的认知里,都认为终端下的Linux操作界面是没有屏保的,只有像windows那样的图形界面下才有屏保.但是其实Linux下也是有屏保的,只不过是ASC ...
- laravel中资源路由的控制器创建方法:
php artisan make:controller Admin\ArticleController --resource 上面的创建方法是,创建控制器文件夹下的Admin文件下的ArticleCo ...
- HDU 4687 Boke and Tsukkomi 一般图匹配,带花树,思路,输出注意空行 难度:4
http://acm.hdu.edu.cn/showproblem.php?pid=4687 此题求哪些边在任何一般图极大匹配中都无用,对于任意一条边i,设i的两个端点分别为si,ti, 则任意一个极 ...