HDU 3791 Binary Search Tree

HDU 3791 Binary Search Tree
Determine whether two sequences are the same binary search tree sequence.
Input
starts with a number n, (1<=n<=20) means there are n to be judged, and the input ends when n=0.
The next line is a sequence, the sequence length is less than 10, contains (0-9) numbers, and there are no repeated numbers. According to this sequence, a binary search tree can be constructed.
There are n sequences in the next n rows, and the format of each sequence is the same as the first sequence. Please judge whether these two sequences can form the same binary search tree.
Output
If the sequence is the same, output YES, otherwise output NO.
Sample Input
2
567432
543267
576342
0
Sample Output
YES
NO

**Problem solution:** Build a search tree, traverse and 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;
}

Guess you like

Origin blog.csdn.net/qq_40924271/article/details/109399419