二叉排序树
来源:互联网 发布:网络被攻击了怎么办 编辑:程序博客网 时间:2024/06/16 06:00
二叉排序树
Time Limit: 1000MS Memory limit: 65536K
题目描述
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树。 今天我们要判断两序列是否为同一二叉排序树
输入
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉排序树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉排序树。(数据保证不会有空树)
输出
示例输入
21234567899876543214321567890
示例输出
NONO
提示
来源
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>using namespace std;int flag;char s[15];struct node{ char data; struct node * l; struct node * r;};void bst(struct node * &p, char c){ if(p == NULL) { p = (struct node *)malloc(sizeof(struct node)); p->data = c; p->l = NULL; p->r = NULL; } else if(p->data > c) bst(p->l, c); else bst(p->r, c);}struct node * creat(char s[], int n){ struct node * p; p = NULL; int i = 0; while(i < n) { bst(p, s[i]); i++; } return p;};void cmp(struct node * tree1, struct node * tree2){ if(tree1 == NULL && tree2 == NULL) { return; } if(tree1 == NULL || tree2 == NULL) { flag = 0; return; } if(tree1->data != tree2->data) { flag = 0; return; } else { cmp(tree1->l, tree2->l); cmp(tree1->r, tree2->r); }}int main(){ int n; while(cin >> n && n != 0) { struct node * tree1, * tree2; cin >> s; tree1 = creat(s, strlen(s)); while(n--) { flag = 1; cin >> s; tree2 = creat(s, strlen(s)); cmp(tree1, tree2); if(flag) cout << "YES" << endl; else cout << "NO" << endl; } } return 0;}
0 0
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- android上WebView设置cookie,以及设置webview cookie在部分手机失效
- OJ-------字符统计**(自带比较)
- 架构师之路--服务器集群搭建、管理、与快速部署
- 转载Android Audio架构
- 第一篇技术博客,人生的第一篇博客!
- 二叉排序树
- 用Java模仿银行自助存取一体机(ATM)
- NOIPの模拟_2016_8_11_t2_种树
- 原根
- jQueryAJAX的$.ajax()用jsonp解决跨域问题
- openGL学习
- 听说每个人都会写单例,你会了吗?
- 9.bash2
- ACdream1726