7-4 Structure of a Binary Tree (30 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.
Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:
Note:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 103 and are separated by a space.
Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A
and B
in the statements are in the tree.
For each statement, print in a line Yes
if it is correct, or No
if not.
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Yes
No
Yes
No
Yes
Yes
Yes
#include
using namespace std;
struct Node{
int data;
int parent;
int level;
bool isleft;
}node[50];
int pre[50],in[50],post[50];
int num=0;
int n;
unordered_map premap,childnum;
void pretra(int root,int start,int end,int level,int parent,bool isleft){
if (start>end) return;
int i=start;
while(i>n;
char s[100];
for(i=0;i>post[i];
for(i=0;i>in[i];
for(i=0;i<=n;i++){
node[i].parent=-1;
node[i].level=-1;
}
pretra(n-1,0,n-1,1,-1,false);
cin>>m;
getchar();
bool isfulln=isfull();
for(i=0;i15){
if(issamelevel(l,r)) cout<<"Yes"<