Description:
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:
- A is the root
- A and B are siblings
- A is the parent of B
- A is the left child of B
- A is the right child of B
- A and B are on the same level
- It is a full tree
Note:
- Two nodes are on the same level, means that they have the same depth.
- A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤), 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 1 and are separated by a space.
Then another positive integer M (≤) is given, followed by M lines of statements. It is guaranteed that both
A
andB
in the statements are in the tree.
Output Specification:
For each statement, print in a line
Yes
if it is correct, orNo
if not.
Sample Input:
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
Sample Output:
Yes No Yes No Yes Yes Yes
Keys:
- 二叉树的存储和遍历
Attention:
- sscanf(s.c_str(), "%d %*s %d", &a, &b);
- s.find("s") != string::npos;
- map<int,node*> mp;
Code:
1 #include<cstdio> 2 #include<string> 3 #include<map> 4 #include<iostream> 5 using namespace std; 6 const int M=1e4; 7 struct node 8 { 9 int data; 10 node *lchild,*rchild; 11 }; 12 int n,pt[M],in[M],layer[M],isfull=1,fa[M]; 13 map<int,node*> mp; 14 15 node *Create(int inL, int inR, int ptL, int ptR, int high, int father) 16 { 17 if(inL > inR) 18 return NULL; 19 node *root = new node; 20 root->data = pt[ptR]; 21 fa[root->data]=father; 22 layer[root->data]=high; 23 int k; 24 for(k=inL; k<=inR; k++) 25 if(in[k] == pt[ptR]) 26 break; 27 int numLeft = k-inL; 28 root->lchild = Create(inL,k-1,ptL,ptL+numLeft-1,high+1,root->data); 29 root->rchild = Create(k+1,inR,ptL+numLeft,ptR-1,high+1,root->data); 30 if((root->lchild==NULL&&root->rchild!=NULL)||(root->lchild!=NULL&&root->rchild==NULL)) 31 isfull=0; 32 mp[root->data]=root; 33 return root; 34 } 35 36 int main() 37 { 38 #ifdef ONLINE_JUDGE 39 #else 40 freopen("Test.txt", "r", stdin); 41 #endif // ONLINE_JUDGE 42 43 int n,m,v1,v2; 44 scanf("%d", &n); 45 for(int i=0; i<n; i++) 46 scanf("%d", &pt[i]); 47 for(int i=0; i<n; i++) 48 scanf("%d", &in[i]); 49 node *root = new node; 50 root = Create(0,n-1,0,n-1,1,-1); 51 scanf("%d\n", &m); 52 string s; 53 for(int i=0; i<m; i++) 54 { 55 getline(cin,s); 56 if(s.find("root")!=string::npos) 57 { 58 sscanf(s.c_str(),"%d is the root", &v1); 59 if(v1 == pt[n-1]) printf("Yes\n"); 60 else printf("No/n"); 61 } 62 else if(s.find("siblings")!=string::npos) 63 { 64 sscanf(s.c_str(),"%d and %d are siblings", &v1,&v2); 65 if(fa[v1]==fa[v2]) printf("Yes\n"); 66 else printf("No\n"); 67 } 68 else if(s.find("parent")!=string::npos) 69 { 70 sscanf(s.c_str(),"%d is the parent of %d", &v1,&v2); 71 if(fa[v2]==v1) printf("Yes\n"); 72 else printf("No\n"); 73 } 74 else if(s.find("left")!=string::npos) 75 { 76 sscanf(s.c_str(),"%d is the left child of %d", &v1,&v2); 77 if(mp[v2]->lchild!=NULL && mp[v2]->lchild->data==v1) printf("Yes\n"); 78 else printf("No\n"); 79 } 80 else if(s.find("right")!=string::npos) 81 { 82 sscanf(s.c_str(),"%d is the right child of %d", &v1,&v2); 83 if(mp[v2]->rchild!=NULL && mp[v2]->rchild->data==v1) printf("Yes\n"); 84 else printf("No\n"); 85 } 86 else if(s.find("same")!=string::npos) 87 { 88 sscanf(s.c_str(),"%d and %d are on the same level", &v1,&v2); 89 if(layer[v1]==layer[v2]) printf("Yes\n"); 90 else printf("No\n"); 91 } 92 else if(s.find("full")!=string::npos) 93 if(isfull) printf("Yes\n"); 94 else printf("No\n"); 95 } 96 97 return 0; 98 }