Home > Back-end >  The algorithm of notes codeup9.4 B, half points
The algorithm of notes codeup9.4 B, half points

Time:09-18

Question B: binary search tree
[proposition: external import]
Time limit: 1.000 SEC memory limit: 32 MB

Title description
Determine whether two sequences for the same sequence of binary search tree

Enter
Start a number n (1 & lt;=nThe next line is a sequence, the sequence length is less than 10, contains (0 ~ 9) number, no repeat Numbers, according to the sequence can be constructed out of a binary search tree,
The next n lines have n sequences, each sequence format like the first sequence, please judge whether the two sequences to form the same binary search tree,

O
If the sequence is the same output YES, otherwise NO output

The sample input Copy
6
45021
12045
54120
45021
45012
21054
50412
0
Sample output Copy
NO
NO
YES
NO
NO
NO
Code:
# include & lt; Bits/stdc++. H>
using namespace std;

Int A, [10].
Int A_size;

Void shuhua (int temp)
{
A_size=0;
Do
{
A [A_size]=temp % 10;
A_size + +;
Temp/=10;
} while (temp!=0);
The reverse (A, A + A_size);
}

Struct node
{
int data;
Node * lchild;
Node * rchild;
};

NewNode node * (int x)
{
Node * p=new node;
P - & gt; Data=https://bbs.csdn.net/topics/x;
P - & gt; Lchild=p - & gt; Rchild=NULL;
Return the p;
}

Void insert (node * & amp; Root, int x)
{
If (root==NULL)
{
Root=newNode (x);
return;
}
If (x & lt;=root - & gt; Data)
Insert (root - & gt; Lchild, x);
Else if (x> Root - & gt; Data)
Insert (root - & gt; Rchild, x);
}

Void creat (node * & amp; Root)
{
For (int I=1; I & lt; A_size; I++)
{
Insert (root, A [I]);
}
}

Void preorder (node * root, vector & V)
{
If (root==NULL) return;
Valerie plame ush_back (root - & gt; The data);
Preorder (root - & gt; Lchild, v);
Preorder (root - & gt; Rchild, v);
}

Void inorder (node * root, vector & V)
{
If (root==NULL) return;

Inorder (root - & gt; Lchild, v);
Valerie plame ush_back (root - & gt; The data);
Inorder (root - & gt; Rchild, v);
}

Void deleteTree (node * & amp; Root)
{
If (root==NULL) return;
DeleteTree (root - & gt; Lchild);
DeleteTree (root - & gt; Rchild);
Delete the root;
}

Int main (void)
{
Freopen (" input. TXT ", "r", stdin);
Int n, temp;
Vector Pre1 pre2, in1 and in2.
While (the scanf (" % d ", & amp; N)!=EOF& & N!=0)
{
//root=NULL;
The scanf (" % d ", & amp; Temp);
Shuhua (temp);
Root1=newNode node * (A [0]);

Creat (root1);
Preorder (root1, pre1);
Inorder (root1, in1);
For (int I=0; I & lt; n; I++)
{
Pre2. The clear ();
In2. The clear ();
//root=NULL;
The scanf (" % d ", & amp; Temp);
Shuhua (temp);
Root2=newNode node * (A [0]);
Creat (root2);
Preorder (root2, pre2);
Inorder (root2, in2);
//if (pre1==pre2 & amp; & In1==in2) printf (" YES \ n ");
//else printf (" \ n ");
Puts (pre1==pre2 & amp; & In1==in2? "YES" : "NO");
//printf (" before del \ n ");
//deleteTree (root2);
//printf (" delete finish \ n ");
}
}

return 0;
}
Don't know where I went wrong,
  • Related