04-Tree 4 Whether the same binary search tree (25 points) given an insertion sequence, you can uniquely identify a binary search tree. However, a given two-fork search tree can be obtained from a number of different insertion sequences. For example, insert the initial empty 3-fork search tree by sequence {2, 1, 3}, and {2, 1, two}, all with the same result. So for the various insertion sequences of the input, you need to determine whether they can generate the same two-fork search tree. Input format: The input contains several sets of test data. The 1th row of each set of data gives two positive integers n (≤10) and L, respectively, the number of elements to be inserted into each sequence and the number of sequences to be inspected. The 2nd line gives the N space-delimited positive integers as the initial insert sequence. The last L line, each row gives n inserted elements, belongs to the L need to check the sequence. For simplicity's sake, we guarantee that each insertion sequence is a 1 to n arrangement. When read n is 0 o'clock, the flag input ends and this set of data is not processed. Output format: For each set of sequences that need to be checked, if the resulting two-fork search tree is the same as the corresponding initial sequence, output "Yes", otherwise output "No". Input Sample: 4 23 1 4 23 4 1 23 2 4 12 12 11 20 Output Example: Yesnono
#include <cstdio>#include<stdlib.h>typedefstructTreeNode *Tree;structtreenode{intdata; intFlag; Tree left,right;}; Tree NewNode (intv) {Tree T= (Tree)malloc(sizeof(structTreeNode)); T->data=v; T->left=t->right=NULL; T->flag=0; returnT;} Tree Insert (Tree& T,intv) { if(! T) t=NewNode (v); Else{ if(V>t->data) T->right=insert (T->RIGHT,V);//I missed t->right!!!. Else if(V<t->data) T->left=insert (T->LEFT,V);//T->left } returnT;} Tree Maketree (intN) {Tree T; inti,v; scanf ("%d",&v); T=newnode (v);//Create the root node first. for(i=1; i<n;i++) {scanf ("%d",&v); T=Insert (T,V); } returnT;}intCheck (Tree T,intv) { if(T->flag) {//has passed this node, then you can continue to check . if(T->DATA>V)returnCheck (t->left,v); Else if(T->DATA<V)returnCheck (t->right,v); Else return 0; } Else{//have not encountered, if the value is equal, then mark it, the description corresponds. if(t->data==v) {T->flag=1; return 1; } Else return 0;//a search encountered a node that did not appear before, inconsistent }}intJudge (Tree T,intN) { inti,v,flag=0; scanf ("%d",&v); if(V!=t->data) flag=1;//The root node is different, false. Elset->flag=1; for(intI=1; i<n;i++) {scanf ("%d",&v); if((!flag) && (!check (t,v)) flag=1;//before is if (!check (t,v) return 0; This causes the subsequent number of inputs to be transferred to the next set } if(flag)return 0; Else return 1;}voidReset (Tree T) {//Clear Marks if(T->left) Reset (t->Left ); if(T->right) Reset (t->Right ); T->flag=0;}voidFreetree (Tree T) {//Release Tree if(T->left) Freetree (t->Left ); if(T->right) Freetree (t->Right ); Free(T); }intMain () {intn,l; Tree T; scanf ("%d",&N); while(n) {scanf ("%d",&l); T=maketree (n); for(intI=0; i<l;i++){ if(Judge (T,n)) printf ("yes\n"); Elseprintf"no\n"); Reset (T); } freetree (T); scanf ("%d", &n);//a scanf is added to the back to make the program loop. If you enter 0, the program can be terminated. } return 0;}
Whether the same binary search tree