Determine whether the same binary search tree (binary search tree)

04- Whether tree 4 is the same binary search tree (25 points)

A binary search tree can be uniquely determined given an insertion sequence. However, a given binary search tree can be derived from many different insertion sequences. For example, inserting into an initially empty binary search tree according to the sequence {2, 1, 3} and {2, 3, 1} respectively, will get the same result. So for the various insertion sequences of the input, you need to determine whether they can generate the same binary search tree.

Input format:

The input contains several sets of test data. Row 1 of each set of data gives two positive integersN (1 0 ) andL , respectively the number of elements inserted in each sequence and the number of sequences to be checked. line 2 givesN space-separated positive integers as the initial insertion sequence. finallyL lines, each givesN inserted elements, belonging toL sequences to check.

For simplicity, we guarantee that each insertion sequence is 1 toA permutation of N. when readWhen N is 0, the input of the flag is over, and this group of data should not be processed.

Output format:

For each set of sequences to be checked, if the binary search tree generated by it is the same as that generated by the corresponding initial sequence, output "Yes", otherwise output "No".

Input sample:

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

Sample output:

Yes
No
No

To the effect: first give a binary tree search tree as the initial tree, and then give other binary search trees to determine whether the new binary tree is the same as the initial binary tree

Attach the AC code:

#include <cstdio>   
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <stack>
#include <queue>
using namespace std;
#define ll long long
typedef struct Tree{
	int data;
	Tree *l,*r;
}*BiTree;
bool check[20];
void build(BiTree &T,int e)
{
	if(!T)
	{
		T = new Tree;
		T->data = e;
		T->l = T->r = NULL;
		return;
	}
	if(!T->l && e < T->data)
	{
		/*BiTree temp = new Tree;
		temp->l = temp->r = NULL;
		T->l = temp;
		temp->data = e;*/
		T->l = new Tree;
		T->l->l = T->l->r = NULL;
		T->l->data = e;
		return;
	}
	if(!T->r && e > T->data)
	{
		/*BiTree temp = new Tree;
		temp->l = temp->r = NULL;
		T->r = temp;
		temp->data = e;*/
		T->r = new Tree;
		T->r->l = T->r->r = NULL;
		T->r->data = e;
		return;
	}
	if(e > T->data)
		build(T->r,e);
	else
		build(T->l,e);
	return;
}
bool juge(BiTree T,int e)
{
	if(!check[T->data] && e != T->data)
		return false;
	check[T->data] = 1;
	if(e == T->data)
		return true;
	if(e < T->data)
		return judge (T-> l, e);
	else
		return juge(T->r,e);
}
intmain()
{
	int n,l;
	while(cin >> n&&n)
	{
		cin >> l;
		BiTree T ;
		T = NULL;
		for(int i = 0;i < n;i++)
		{
			int x;
			cin >> x;
			build(T,x);
		}
		while(l--)
		{
			int f = 0;
			memset(check,0,sizeof(check));
			for(int i = 0;i < n;i++)
			{
				int x;
				cin >> x;
				if(!juge(T,x))
					f = 1;
			}
			if(f)
				cout << "No" <<endl;
			else
				cout << "Yes" <<endl;
		}
	}
    //cout << "AC" <<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325579273&siteId=291194637