Árbol de búsqueda binaria HDU 3791

HDU 3791 Árbol de búsqueda binaria
Determina si dos secuencias son la misma secuencia de árbol de búsqueda binaria. La
entrada
comienza con un número n, (1 <= n <= 20) significa que hay n para juzgar y la entrada termina cuando n = 0.
La siguiente línea es una secuencia, la longitud de la secuencia es menor que 10, contiene (0-9) números y no hay números repetidos De acuerdo con esta secuencia, se puede construir un árbol de búsqueda binario.
Hay n secuencias en las siguientes n filas y el formato de cada secuencia es el mismo que el de la primera secuencia. Juzgue si estas dos secuencias pueden formar el mismo árbol de búsqueda binaria.
Salida
Si la secuencia es la misma, emita SÍ, en caso contrario, salida NO.
Entrada de muestra
2
567432
543267
576342
0
Salida de muestra

NO

** Solución del problema: ** Cree un árbol de búsqueda, recorra y compare

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define speed {
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 120;
const int INF = 0x3f3f3f3f;
const double esp_0 = 1e-6;
int gcd(int x, int y) {
    
    
	return y ? gcd(y, x % y) : x;
}
int a[maxn], b[maxn], k = 0;
typedef struct Tree* tree;
typedef struct Tree {
    
    
	int data;
	tree l, r;
};
tree root;
tree build(tree root, int x) {
    
    
	if (root == NULL) {
    
    
		root = (tree)malloc(sizeof(struct Tree));
		root->data = x;
		root->l = root->r = NULL;
	}
	else {
    
    
		if (x <= root->data)root->l = build(root->l, x);
		else root->r = build(root->r, x);
	}
	return root;
}
void preorder(tree root) {
    
    
	if (root) {
    
    
		a[k++] = root->data;
		preorder(root->l);
		preorder(root->r);
	}
}
vector<int>v[maxn];
void dfs(tree root, int deep) {
    
    //层序遍历
	if (root == NULL)return;
	v[deep].push_back(root->data);
	dfs(root->l, deep + 1);
	dfs(root->r, deep + 1);
}
int main() {
    
    
	speed;
	int n;
	string s;
	while (cin >> n, n) {
    
    
		root = NULL;
		k = 0;
		cin >> s;
		for (int i = 0; i < s.size(); ++i) {
    
    
			int temp = s[i] - '0';
			root = build(root, temp);
		}
		preorder(root);
		//dfs(root, 0);
		//for (int i = 0; i < s.size(); ++i) {
    
    
		//	for (int j = 0; j < v[i].size(); ++j) {
    
    
		//		cout << v[i][j] << " ";
		//	}
		//}
		//cout << endl;
		for (int i = 0; i < s.size(); ++i) {
    
    
			b[i] = a[i];
		}
		//for (int i = 0; i < s.size(); ++i)cout << b[i] << " ";
		//cout << endl;
		while (n--) {
    
    
			k = 0;
			root = NULL;
			cin >> s;
			for (int i = 0; i < s.size(); ++i) {
    
    
				int temp = s[i] - '0';
				root = build(root, temp);
			}
			preorder(root);
			//for (int i = 0; i < s.size(); ++i)cout << a[i] << " ";
			//cout << endl;
			int i;
			for (i = 0; i < s.size(); ++i) {
    
    
				if (a[i] != b[i]) {
    
    
					cout << "NO" << endl;
					break;
				}
			}
			if (i >= s.size())cout << "YES" << endl;
		}
	}
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/qq_40924271/article/details/109399419
Recomendado
Clasificación