PAT 是否为同一棵二叉搜索树
2016-07-30 11:52
337 查看
#include <iostream>
#include <string.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n,l;
typedef struct dataStruct{
dataStruct* left;
dataStruct* right;
int data;
}*node;
void CreatTree(node &T, int dt){
if(T == NULL){
T = new dataStruct();
T->data = dt;
T->left = NULL;
T->right = NULL;
return ;
}
if(T->data > dt){
CreatTree(T->left, dt);
}else{
CreatTree(T->right, dt);
}
}
bool isEqual(node t1,node t2){
if(t1 != NULL && t2 != NULL){
if(t1->data != t2->data){
return false;
}
return (isEqual(t1->left,t2->left)&&isEqual(t1->right,t2->right));
}else{
if(t1 == NULL && t2 ==NULL){
return true;
}else{
return false;
}
}
}
int main(){
int w;
int len1;
node t1,t2;
while(cin>>n && n != 0){
cin>>l;
t1 = NULL;
for(int i = 0 ;i < n ;i++){
cin>>w;
CreatTree(t1,w);
}
for(int i = 0 ;i < l ;i++){
t2 = NULL;
for(int j = 0 ;j < n ;j++){
cin>>w;
CreatTree(t2, w);
}
if(isEqual(t1,t2)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}
#include <string.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int n,l;
typedef struct dataStruct{
dataStruct* left;
dataStruct* right;
int data;
}*node;
void CreatTree(node &T, int dt){
if(T == NULL){
T = new dataStruct();
T->data = dt;
T->left = NULL;
T->right = NULL;
return ;
}
if(T->data > dt){
CreatTree(T->left, dt);
}else{
CreatTree(T->right, dt);
}
}
bool isEqual(node t1,node t2){
if(t1 != NULL && t2 != NULL){
if(t1->data != t2->data){
return false;
}
return (isEqual(t1->left,t2->left)&&isEqual(t1->right,t2->right));
}else{
if(t1 == NULL && t2 ==NULL){
return true;
}else{
return false;
}
}
}
int main(){
int w;
int len1;
node t1,t2;
while(cin>>n && n != 0){
cin>>l;
t1 = NULL;
for(int i = 0 ;i < n ;i++){
cin>>w;
CreatTree(t1,w);
}
for(int i = 0 ;i < l ;i++){
t2 = NULL;
for(int j = 0 ;j < n ;j++){
cin>>w;
CreatTree(t2, w);
}
if(isEqual(t1,t2)){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
}
return 0;
}
相关文章推荐
- POJ Reduced ID Numbers 同余 暴力
- BJFU 1559 抽奖进阶 【位运算】【思维】
- hdu5775树状数组
- spring
- JCBC-事务
- 【Basic computer】-----GC(gabge cllection)
- Java集合List和Set的原理
- HBase相关论文阅读
- JS权威指南读书笔记(三)
- 一些不错的算法学习练习站点
- PyGobject(四十三)布局容器之InfoBar
- BestCoder Round #84 1004Dertouzos
- Android AsyncTask完全解析,带你从源码的角度彻底理解
- Spring 从零开始(Myeclipse10)
- Visual Studio 2015配置OpenCV环境
- 面向对象(OO)程序设计
- iOS 最新版 CocoaPods 的安装使用
- SSDP协议
- 1099. Build A Binary Search Tree (30)
- C#学习笔记--get/set访问器