PAT 2019-3 7-4 Structure of a Binary Tree
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 /* 2 二叉树中各结点值为互不相同的正整数 3 给出后序遍历和中序遍历 4 5 判断所给语句是否正确 6 根节点 7 兄弟 8 父结点 9 左孩子 10 右孩子 11 同一层 12 满二叉树 13 14 基本思路: 15 建树,存储<键值,结点>的映射,存储结点所在层次,判断是否为满二叉树 16 sscanf读取字符串 17 */ 18 #include<cstdio> 19 #include<string> 20 #include<unordered_map> 21 #include<iostream> 22 using namespace std; 23 const int M=1e3+10; 24 struct node 25 { 26 int data,high; 27 node *lchild,*rchild; 28 }; 29 int in[M],pt[M],hs[M]={0},fa[M]={0},isfull=1; 30 unordered_map<int,node*> mp; 31 32 node *Create(int ptL, int ptR, int inL, int inR, int layer, int father) 33 { 34 if(ptL > ptR) 35 return NULL; 36 node *root = new node; 37 root->data = pt[ptR]; 38 root->high = layer; 39 int k; 40 for(k=inL; k<=inR; k++) 41 if(in[k]==pt[ptR]) 42 break; 43 int numLeft = k-inL; 44 root->lchild = Create(ptL,ptL+numLeft-1,inL,k-1,layer+1, root->data); 45 root->rchild = Create(ptL+numLeft,ptR-1,k+1,inR,layer+1, root->data); 46 hs[root->data]=1; 47 fa[root->data]=father; 48 mp[root->data] = root; 49 if((root->lchild==NULL&&root->rchild!=NULL) || (root->lchild!=NULL&&root->rchild==NULL)) 50 isfull=0; 51 return root; 52 } 53 54 int main() 55 { 56 #ifdef ONLINE_JUDGE 57 #else 58 freopen("Test.txt", "r", stdin); 59 #endif // ONLINE_JUDEG 60 61 int n,m; 62 scanf("%d", &n); 63 for(int i=0; i<n; i++) 64 scanf("%d", &pt[i]); 65 for(int i=0; i<n; i++) 66 scanf("%d", &in[i]); 67 68 node *root=Create(0,n-1,0,n-1,1,-1); 69 scanf("%d\n", &m); 70 while(m--) 71 { 72 int v1,v2; 73 string st; 74 getline(cin,st); 75 if(st.find("root") != string::npos) 76 { 77 sscanf(st.c_str(), "%d is the root", &v1); 78 if(v1 == pt[n-1]) 79 printf("Yes\n"); 80 else 81 printf("No\n"); 82 } 83 else if(st.find("siblings") != string::npos) 84 { 85 sscanf(st.c_str(), "%d and %d are siblings", &v1,&v2); 86 if(hs[v1]==1 && hs[v2]==1 && fa[v1]==fa[v2]) 87 printf("Yes\n"); 88 else 89 printf("No\n"); 90 } 91 else if(st.find("parent") != string::npos) 92 { 93 sscanf(st.c_str(), "%d is the parent of %d", &v1,&v2); 94 if(hs[v1]==1 && hs[v2]==1 && fa[v2]==v1) 95 printf("Yes\n"); 96 else 97 printf("No\n"); 98 } 99 else if(st.find("left") != string::npos) 100 { 101 sscanf(st.c_str(), "%d is the left child of %d", &v1,&v2); 102 if(hs[v1]==1 && hs[v2]==1 && mp[v2]->lchild==mp[v1]) 103 printf("Yes\n"); 104 else 105 printf("No\n"); 106 } 107 else if(st.find("right") != string::npos) 108 { 109 sscanf(st.c_str(), "%d is the right child of %d", &v1,&v2); 110 if(hs[v1]==1 && hs[v2]==1 && mp[v2]->rchild==mp[v1]) 111 printf("Yes\n"); 112 else 113 printf("No\n"); 114 } 115 else if(st.find("same") != string::npos) 116 { 117 sscanf(st.c_str(), "%d and %d are on the same level", &v1,&v2); 118 if(hs[v1]==1 && hs[v2]==1 && mp[v1]->high==mp[v2]->high) 119 printf("Yes\n"); 120 else 121 printf("No\n"); 122 } 123 else if(st.find("full") != string::npos) 124 { 125 if(isfull) 126 printf("Yes\n"); 127 else 128 printf("No\n"); 129 } 130 } 131 132 return 0; 133 }