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 ...
随机推荐
- JAVA_返回一个数值的相反数的几种方式.
一个方法接收一个int类型值,需要返回它的相反数. 如传入1,返回-1 传入-22,返回22 最简单的方式是return 0-number; 还有其他方式: public class Kata { p ...
- Spring之核心容器bean
摘要:Spring的核心容器实现了Ioc,其目 的是提供一种无侵入式的框架.在本文中,首先讲解了Spring的基础bean的相关知识,然后介绍了Spring是如何对bean进行管理的. 在Spring ...
- css3 抖动
1. html <div id="wrapper"> <section> <p class="shake freez shake-hard& ...
- Java Spring-注解进行属性注入
2017-11-06 21:19:43 一.Spring的注解装配BeanSpring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供 ...
- IntelliJ IDEA自定义类和方法注解模板
现在Java开发主流工具应该是Intelij Idea 方便快捷. 本文将主要介绍如何用Intelij Idea配置类及方法的注释模板提高代码注释效率 1. 配置类注解模板 找到配置页面 File - ...
- C#通过WMI读取MAC地址
该方法依赖WMI的系统服务,该服务一般不会被关闭;但如果系统服务缺失或者出现问题,该方法无法取得MAC地址,需要重启Windows Management Instrumentation服务. publ ...
- 用Python操作Named pipe命名管道,实用做法——os.read 或 os.write
https://blog.csdn.net/mayao11/article/details/50618598
- Linux命令详解-info
info是一种文档格式,也是阅读此格式文档的阅读器:我们常用它来查看Linux命令的info文档.它以主题的形式把几个命令组织在一起,以便于我们阅读:在主题内以node(节点)的形式把本主题的几个命令 ...
- UVA-1515 Pool construction (最小割)
题目大意:有一块地,分成nxm块.有的块上长着草,有的块上是荒地.将任何一块长着草的块上的草拔掉都需要花费d个力气,往任何一块荒地上种上草都需要花费f个力气,在草和荒地之间架一个篱笆需要花费b个力气, ...
- module.exports和exports
require 用来加载代码,而 exports 和 module.exports 则用来导出代码.但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 e ...