您的位置:首页 > 其它

HDU 3791 比较两棵二叉搜索树是否相同(建树,比较)

2016-09-03 16:32 295 查看


二叉搜索树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2538    Accepted Submission(s): 1102


Problem Description

判断两序列是否为同一二叉搜索树序列

Input

开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。

接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。

接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。

Output

如果序列相同则输出YES,否则输出NO

Sample Input

2
567432
543267
576342
0

Sample Output

YES
NO

Source

浙大计算机研究生复试上机考试-2010年

慕课上的视频教程

对于比较BST是否相同,有以下三种方法

1.分别建两棵搜索树进行比较

2.不建树的方法

比如2143 和2413 

可以看出2是根节点,然后第一组比2小的为1,比2大的为43; 第二组也是,所以这两棵二叉树相同.

3.建一棵树,再判别其他序列是否与该树一致(会在每个节点用到一个flag进行标记)

思路:

这道题的数据量很小,所以可以直接采用第一种方法

(下面用三种方法实现这个问题)

下面是第一种方法

//hdu 3791 两棵二叉搜索树的比较
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
//http://blog.csdn.net/linraise/article/details/19573361
//这道题不知道二叉树的长度,所以应该用字符串储存
struct BST{
char key;
BST *left;
BST *right;
BST(char x):key(x),left(NULL),right(NULL){}
//	BST(){}
//	BST(int k){
//		key=x;left=right=NULL;
//	}
};

void insert (BST *&root1,char key)
{
if (key<root1->key)
{
if (root1->left)
insert(root1->left,key);
else
root1->left=new BST(key);
}
else
{
if (root1->right)
insert(root1->right,key);
else
root1->right=new BST(key);
}
}

void Del(BST* root)//这段是可以不要的
{
if (root->left) Del(root->left);
if (root->right) Del(root->right);
delete root;
//	if (root)
//	{
//		Del(root->left);
//		Del(root->right);
//		delete root;
//	}
}

bool isequal(BST* root1,BST* root2)
{
if (root1&&root2)
{
if (root1->key!=root2->key)
return false;
return (isequal(root1->left,root2->left))&&(isequal(root1->right,root2->right));
}
else if (!root1 && !root2)
return true;
else
return false;
}

int main()
{
int n;
string line;
while (~scanf("%d%*c",&n),n)
{
getline(cin,line);
BST *root1=new BST(line[0]);
for (int i=1;i<line.size();i++)
insert(root1,line[i]);
BST *root2=NULL;
while (n--)
{
getline(cin,line);
root2=new BST(line[0]);
for (int i=1;i<line.size();i++)
insert(root2,line[i]);
if (isequal(root1,root2))
puts("YES");
else
puts("NO");
}
}
return 0;
}


下面是第二种方法

//hdu 3791 两棵二叉搜索树的比较
//(不用建树的方法)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
string line;
string line1,line2;
string line3,line4;
//因为string 还不是很熟悉,不知道怎么传递参数...所以只能暂时设为全局变量这样用了...
void insert1()
{
for (int i=1;i<line.size();i++)
{
if (line[i]<line[0])
line1.push_back(line[i]);
else
line2.push_back(line[i]);
}
/*
string::iterator it=line.begin();
char x=line.begin();
for (it++;it!=line.end();it++)
{
if (*it<x)
str1.push_back(it);
else
str2.push_back(it);
}
*/
}

void insert()
{
for (int i=1;i<line.size();i++)
{
if (line[i]<line[0])
line3.push_back(line[i]);
else
line4.push_back(line[i]);
}
}

bool isequal(string str1,string str2)
{
if (str1==str2) return true;
else return false;
}
int main()
{
int n;

while (~scanf("%d%*c",&n),n)
{
getline(cin,line);//输入一行字符
line1.clear();
line2.clear();
insert1();
while (n--)
{
getline(cin,line);
line3.clear();
line4.clear();
insert();
if ((isequal(line1,line3))&&(isequal(line2,line4))) puts("YES");
else puts("NO");
}
}
return 0;
}


下面是解法三

//hdu 3791 两棵二叉搜索树的比较
//(只建一棵树)
//建树+遍历
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
struct BST{
char key;
BST *left;
BST *right;
int flag;
BST(char x):key(x),left(NULL),right(NULL),flag(0){}
};
void insert(BST *&root,char key)
{
if (key<root->key)
{
if (root->left)
insert(root->left,key);
else
root->left=new BST(key);
}
else
{
if (root->right)
insert(root->right,key);
else
root->right=new BST(key);
}
}
void reset(BST *&root)
{
if (root->left) reset(root->left);
if (root->right) reset(root->right);
root->flag=0;
}
bool isequal(BST *root,char key)
{
if (root->flag)
{
if (key<root->key)
return isequal(root->left,key);
else if (key>root->key)
return isequal(root->right,key);
else return false;
}
else
{
if (root->key==key)
{
root->flag=1;
return true;
}
else return false;
}
}
int main()
{
int n;
string line;
while (~scanf("%d%*c",&n),n)
{
getline(cin,line);
BST *root=new BST(line[0]);
for (int i=1;i<line.size();i++)
insert(root,line[i]);
while (n--)
{
reset(root);//一开始要节点的flag都置0
bool flag=true;
getline(cin,line);
for (int i=0;i<line.size();i++)
{
if (!isequal(root,line[i]))
{
puts("NO");
flag=false;
break;
}
}
if (flag==true)	puts("YES");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息