04- 4 whether the tree with a binary search tree (25 points) binary search tree structure

Given a sequence of insertion can be uniquely determined a binary search tree. However, given a binary search tree, but can be obtained from a variety of different insert sequence. 2, respectively, according to a sequence {e.g., 1, 3} and {2, 3, 1} is inserted into an initially empty binary search tree, the same results were obtained. Thus the insertion sequence for the various input, you need to determine whether they are able to generate the same binary search tree.

Input formats:

Comprising a plurality of groups of test data input. Each row of the first data is given two positive integers N ( ≤) and L, respectively, the sequence number is the number of each sequence and the inserted element to be inspected. Line 2 shows a space-separated positive integer number N, as the initial insertion sequence. Finally L rows, each row gives N insertion element belonging to the L sequence needs to be checked.

For simplicity, we ensure that each insertion sequence to both 1 a permutation of N. When the read N is 0, the end flag is input, not the set of data processing.

Output formats:

If it generates a binary search tree with the corresponding initial sequence generated by the same sequence each group needs to be checked, output "Yes", otherwise a "No".

Sample input:

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
 
 
   

ps: the original structure of a tree, flag used to identify different trees

#include <iostream>
using namespace std;

struct Node
{
    int num;
    Node * Left;
    Node * Right;
    int flag;
    Node(int n_,Node* p1,Node* p2,int f_):num(n_),Left(p1),Right(p2),flag(f_){}
};

Node* insert_(int t,Node* root)
{
    if(root==NULL) {
        root = new Node(t,NULL,NULL,0);
    }
    else if(t > (root->num)) {//往右
        root->Right = insert_(t,root->Right); //root-Right=...
    }
    else if(t < (root->num)) {//往左
        root->Left = insert_(t,root->Left);
    }
    return root;
}

Node * build(int n)
{
    Node* root=NULL;
    while(n--) {
        int t; cin>>t;
        root = insert_(t,root);
    }
    return root;
}

int check(intt, the Node * the root) // the tag found t In Flag 
{
     IF (directory root-> NUM == t) { 
        the root -> In Flag = . 1 ;
         return  0 ; 
    } 
    the else  IF (directory root-> NUM> t) {
         IF ( ! directory root-> in flag) { // path found no marked 
            return  . 1 ; //
         }
         the else 
            return Check (T, directory root-> Left); 
    } 
    the else {
         IF (directory root-> in flag) {! // path not found tag 
            return  . 1 ; //
        }
        else
            return check(t,root->Right);
    }
}

void Reset(Node* root)
{
    if(root!=NULL) {
        root->flag=0;
        Reset(root->Left);
        Reset(root->Right);
    }
}

void FreeTree(Node* root)
{
    if(root->Left) FreeTree(root->Left);
    if(root->Right) FreeTree(root->Right);
    delete root;
}

int main()
{
    int N,L;
    while(1)
    {
        cin>>N;
        if(!N) return 0;
        cin>>L;
        Node* root=build(N);
        while(L--)
        {
            int flag=0;
            for(int i=0;i<N;i++)
            {
                int t; cin>>t;
                if(check(t,root)) flag=1;
            }
            if(flag) cout<<"No\n";
            else cout<<"Yes\n";

            Reset(root);
        }
        FreeTree(root);

    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/liuyongliu/p/12449986.html
Recommended