二叉排序树 (sdut oj 2482)
来源:互联网 发布:mac豆沙色口红推荐 编辑:程序博客网 时间:2024/05/22 07:40
二叉排序树
Time Limit: 1000MS Memory limit: 65536K
题目描述
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
输入
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉排序树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉排序树。(数据保证不会有空树)
输出
示例输入
21234567899876543214321567890
示例输出
NONO
提示
建好排序树,剩下判断是否为空树,元素是否相等,然后递归左子树, 递归右子树;
示例程序
#include <bits/stdc++.h>using namespace std;struct node{ char data; node *lchild, *rchild;} *root1, *root2;char a[25];int flag;void Insert(node *(&root), char t){ if(!root) { root = new node; root->data = t; root->lchild = NULL; root->rchild = NULL; } else { if(root->data > t) Insert(root->lchild, t); else Insert(root->rchild, t); }}node *creat(int n, char a[]){ struct node *root; root = NULL; for(int i = 0; i < n; i++) Insert(root, a[i]); return root;}void campare(node *root1, node *root2){ if(root1 == NULL&&root2 == NULL) return; else if(root1 == NULL||root2 == NULL) { flag = 0; return ; } else if(root1->data != root2->data) { flag = 0; return ; } else { campare(root1->lchild, root2->lchild); campare(root1->rchild, root2->rchild); }}int main(){ int n; while(cin >> n) { if(n == 0) return 0; cin >> a; int l = strlen(a); root1 = creat(l, a); while(n--) { flag = 1; cin >> a; root2 = creat(strlen(a), a); campare(root1, root2); if(flag) cout << "YES" << endl; else cout << "NO" << endl; } } return 0;}
0 0
- 二叉排序树 (sdut oj 2482)
- SDUT oj 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- SDUT 2482 二叉排序树
- <sdut-ACM> 2482二叉排序树
- sdut 二叉排序树
- SDUT-二叉排序树
- 数据结构实验之查找一:二叉排序树 (sdut oj 3373)
- SDUT oj 数据结构实验之查找一:二叉排序树
- sdut oj2482 二叉排序树
- 二叉排序树——SDUT
- 【北邮OJ】97. 二叉排序树
- SDUT OJ 2718
- SDUT OJ -2892 A
- 形参的改变如何影响实参
- JAVA中对null进行强制类型转换
- 最长公共子序列(动态规划)
- coreseek中文搜索
- Informatica PowerCenter 常用转换组件一览表
- 二叉排序树 (sdut oj 2482)
- QT tcp Socket 通信开发
- PopWindow的使用
- Redis (error) NOAUTH Authentication required.解决方法
- lamp环境的搭建和配置
- Git如何撤销merge操作
- QT helloworld
- 存储型跨站脚本攻击
- android内存性能数据获取--api篇