19年春季第四题 PAT甲级 1159 Structure of a Binary Tree(30分)

柔光的暖阳◎ 2023-07-07 15:58 98阅读 0赞

汇总贴

2020年3月PAT甲级满分必备刷题技巧

题目

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 (≤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 10​^3​​ 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.

Output Specification:

For each statement, print in a line Yes if it is correct, or No if not.

Sample Input:

  1. 9
  2. 16 7 11 32 28 2 23 8 15
  3. 16 23 7 32 11 2 28 15 8
  4. 7
  5. 15 is the root
  6. 8 and 2 are siblings
  7. 32 is the parent of 11
  8. 23 is the left child of 16
  9. 28 is the right child of 2
  10. 7 and 11 are on the same level
  11. It is a full tree

Sample Output:

  1. Yes
  2. No
  3. Yes
  4. No
  5. Yes
  6. Yes
  7. Yes

题目分析

1.postorder and inorder traversal sequences
给定后序和中序,这个是PAT甲级题库中很常见的知识点“二叉树的遍历”,可以参考《算法笔记》9.2节和题库的1086、1119、1138。

2.因为可能不是完全二叉树,同时输出的东西也挺复杂的,所以应该果断用DFS建二叉树。建树方法略有不同于1135(https://www.liuchuo.net/archives/4099),是采用map和结构体。

3.本题有7种输出,对应7个数据,都可以在DFS建树中体现:
(1)root,建树的返回就是root的值
(2)siblings,等价于两个节点的祖先是同一个
(3)parent,每个节点存父亲的值
(4)leftchild,A节点的左孩子是B,为了访问A的时候就能读出B节点的值,要用map和结构体
(5)rightchild,B节点的左孩子是A
(6)same level,记录节点的深度
(7)full tree,记录节点有没有左右孩子

满分代码

来源致谢:https://blog.csdn.net/lianwaiyuwusheng/article/details/88084378

  1. #include<iostream>
  2. #include<vector>
  3. #include<cstdio>
  4. #include<set>
  5. #include<map>
  6. #include<algorithm>
  7. using namespace std;
  8. #define MAX 40
  9. struct node{
  10. int l,r,d,p;
  11. node(){}
  12. node(int l,int r,int d,int p):l(l),r(r),d(d),p(p){}
  13. };
  14. map<int,node>arr;
  15. int po[MAX],in[MAX],N,A,B;
  16. bool flag=0;
  17. string str;
  18. int tree(int poR,int inL,int inR,int de,int pa)
  19. {
  20. if(inL>inR)return -1;
  21. int i=inL;
  22. while(in[i]!=po[poR])++i;
  23. arr[po[poR]].p=pa;
  24. arr[po[poR]].d=de;
  25. arr[po[poR]].l=tree(poR-(inR-i)-1,inL,i-1,de+1,po[poR]);
  26. arr[po[poR]].r=tree(poR-1,i+1,inR,de+1,po[poR]);
  27. if((arr[po[poR]].r==-1&&arr[po[poR]].l!=-1)||(arr[po[poR]].r!=-1&&arr[po[poR]].l==-1))flag=1;
  28. return po[poR];
  29. }
  30. int input()
  31. {
  32. int i;
  33. cin>>str;
  34. if(str[0]>='0'&&str[0]<='9'){
  35. i=0;A=0;
  36. while(i<str.length()){A=A*10+str[i]-'0';++i;}
  37. cin>>str;
  38. if(str=="and"){
  39. scanf("%d",&B);
  40. cin>>str;cin>>str;
  41. if(str=="siblings"){
  42. return 2;
  43. }else{
  44. i=3;
  45. while(i--){cin>>str;}
  46. return 6;
  47. }
  48. }else{
  49. cin>>str;cin>>str;
  50. if(str=="root")return 1;
  51. else if(str=="parent"){
  52. cin>>str;scanf("%d",&B);
  53. return 3;
  54. }else if(str=="left"){
  55. cin>>str;cin>>str;scanf("%d",&B);
  56. return 4;
  57. }else if(str=="right"){
  58. cin>>str;cin>>str;scanf("%d",&B);
  59. return 5;
  60. }
  61. }
  62. }else{
  63. i=4;
  64. while(i--)cin>>str;
  65. return 7;
  66. }
  67. }
  68. int main()
  69. {
  70. //freopen("test.txt","r",stdin);
  71. scanf("%d",&N);
  72. for(int i=0;i<N;++i)scanf("%d",&po[i]);
  73. for(int i=0;i<N;++i)scanf("%d",&in[i]);
  74. int root=tree(N-1,0,N-1,0,-1);
  75. scanf("%d",&N);
  76. while(N--){
  77. int x=input();
  78. bool f=0;
  79. if(x==1){
  80. if(A!=root)f=1;
  81. }else if(x==2){
  82. if(arr[A].p!=arr[B].p)f=1;
  83. }else if(x==3){
  84. if(arr[B].p!=A)f=1;
  85. }else if(x==4){
  86. if(arr[B].l!=A)f=1;
  87. }else if(x==5){
  88. if(arr[B].r!=A)f=1;
  89. }else if(x==6){
  90. if(arr[B].d!=arr[A].d)f=1;
  91. }else if(x==7){
  92. if(flag)f=1;
  93. }
  94. if(f)printf("No\n");
  95. else printf("Yes\n");
  96. }
  97. return 0;
  98. }

发表评论

表情:
评论列表 (有 0 条评论,98人围观)

还没有评论,来说两句吧...

相关阅读