给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。
输入格式:
输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。最后L行,每行给出N个插入的元素,属于L个需要检查的序列。
简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。
输出格式:
对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。
输入样例:
4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0
输出样例:
Yes
No
No
这题,我想出了两种解法,题意就不说了,关键点是判断同一个二叉树的这个函数,之前说过,当我们学会遍历二叉树后,很多问题只需要把遍历改一改就行了。所以最容易想到的遍历的时候给结点存进一个数组里面,这里我推荐字符串(其实,以后也应该多使用字符串)。第二种也会容易想到就是,两颗树同时遍历,一个一个结点的判断!
第一种解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include<iostream> #include<string> #include<malloc.h> using namespace std; typedef int ElementType; typedef struct tnode* BinTree; struct tnode { ElementType Data; BinTree Left; BinTree Right; }; BinTree Insert(BinTree BST, ElementType X) { if (!BST) { BST = (BinTree)malloc(sizeof(struct tnode)); BST->Data = X; BST->Left = BST->Right = NULL; } else { if (X < BST->Data) BST->Left = Insert(BST->Left, X); else if (X > BST->Data) BST->Right = Insert(BST->Right, X); } return BST; } void preorder(BinTree BST, string& s) { if (BST) { s += BST->Data + '0'; // 将结点值保存进字符串 preorder(BST->Left, s); // 进入左子树 preorder(BST->Right, s); // 进入右子树 } } int main() { int n, k; while (1) { cin >> n; if (!n) break; cin >> k; BinTree BST; BST = NULL; int tmp1; for (int i = 0; i < n; i++) { cin >> tmp1; BST = Insert(BST, tmp1); } string BSTs; preorder(BST, BSTs); while (k--) { BinTree BST2; BST2 = NULL; int tmp2; for (int i = 0; i < n; i++) { cin >> tmp2; BST2 = Insert(BST2, tmp2); } string BST2s; preorder(BST2,BST2s); if (BSTs == BST2s) cout << "Yes" << endl; else cout << "No" << endl; } } } |
第二种解法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
#include<iostream> #include<malloc.h> using namespace std; typedef int ElementType; typedef struct tnode* BinTree; bool flag = true; struct tnode { ElementType Data; BinTree Left; BinTree Right; }; BinTree Insert(BinTree BST, ElementType X) { if (!BST) { BST = (BinTree)malloc(sizeof(struct tnode)); BST->Data = X; BST->Left = BST->Right = NULL; } else { if (X < BST->Data) BST->Left = Insert(BST->Left, X); else if (X > BST->Data) BST->Right = Insert(BST->Right, X); } return BST; } //这个函数是别人告诉我的,我一开始想的是前序遍历存进一个数组里面,也就是第一种方法,ac后我自己思考了一下这个函数, //这个函数是不能作为判断两个搜索二叉树的的标准。。。它有一个局限性,就是二叉树的结点必须一样才可以,后面我写了一个判断所有二叉树的函数 void xx(BinTree BST, BinTree BST2) { if (!BST||!BST2) return; if (BST->Data != BST2->Data) { flag = false; return;//提高效率,发现错误直接退出(●ˇ∀ˇ●) } xx(BST->Left, BST2->Left); xx(BST->Right, BST2->Right); } int main(){ int n, k; while(1){ cin >> n; if (!n) break; cin >> k; BinTree BST; BST = NULL; int tmp1; for (int i = 0; i < n; i++) { cin >> tmp1; BST = Insert(BST, tmp1); } while (k--) { BinTree BST2; BST2 = NULL; int tmp2; for (int i = 0; i < n; i++) { cin >> tmp2; BST2 = Insert(BST2, tmp2); } flag = true; xx(BST,BST2);//flag是全局函数,每次调用改变flag的值从而判断真假 if (flag) cout << "Yes" << endl; else cout << "No" << endl; } } } |
这里说一下,解法二中的判断二叉树的函数并不是完美的,有缺陷,注释我写了说明。有时间的小伙伴欢迎来和博主讨论~