PAT甲级2019春季7-4 1159.Structure of a Binary Tree (30 分)

£神魔★判官ぃ 2023-02-23 12:09 146阅读 0赞

算法笔记总目录
关键英语单词解释
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:

  1. A is the root
  2. A and B are siblings
  3. A is the parent of B
  4. A is the left child of B
  5. A is the right child of B
  6. A and B are on the same level
  7. 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)root,建树的返回就是root的值
(2)siblings,等价于两个节点的祖先是同一个
(3)parent,每个节点存父亲的值
(4)leftchild,A节点的左孩子是B,为了访问A的时候就能读出B节点的值,要用map和结构体
(5)rightchild,B节点的左孩子是A
(6)same level,记录节点的深度
(7)full tree,记录节点有没有左右孩子

注:代码仅通过测试样例

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int val,level;
  5. node *parent, *lc, *rc;
  6. node(int v, int lev) : val(v), parent(NULL), lc(NULL), rc(NULL), level(lev) { }
  7. };
  8. bool isFull = true;
  9. unordered_map<int, node*> mp;
  10. vector<int> post, in;
  11. node *root = NULL;
  12. node *create(int postL,int postR,int inL,int inR,int level){
  13. if(postL > postR) return NULL;
  14. node *root = new node(post[postR],level++);
  15. int k;
  16. for(k=inL;k<=inR;k++){
  17. if(in[k]==post[postR])break;
  18. }
  19. int numleft = k-inL;
  20. root->lc = create(postL,postL+numleft-1,inL,k-1,level);
  21. root->rc = create(postL+numleft,postR-1,k+1,inR,level);
  22. if(root->lc != NULL) root->lc->parent = root;
  23. if(root->rc != NULL) root->rc->parent = root;
  24. if ((root -> lc == NULL && root -> rc != NULL) || (root -> lc != NULL && root -> rc == NULL)) isFull = false;
  25. mp[post[postR]] = root;
  26. return root;
  27. }
  28. bool judge(string s) {
  29. int u, v;
  30. if (s.find("root") != string::npos) {
  31. sscanf(s.c_str(), "%d is the root", &u);
  32. return u == root -> val;
  33. } else if (s.find("siblings") != string::npos) {
  34. sscanf(s.c_str(), "%d and %d are siblings", &u, &v);
  35. return mp[u] -> parent == mp[v] -> parent;
  36. } else if (s.find("parent") != string::npos) {
  37. sscanf(s.c_str(), "%d is the parent of %d", &u, &v);
  38. return mp[v] -> parent == mp[u];
  39. } else if (s.find("left") != string::npos) {
  40. sscanf(s.c_str(), "%d is the left child of %d", &u, &v);
  41. return mp[v] -> lc == mp[u];
  42. } else if (s.find("right") != string::npos) {
  43. sscanf(s.c_str(), "%d is the right child of %d", &u, &v);
  44. return mp[v] -> rc == mp[u];
  45. } else if (s.find("level") != string::npos) {
  46. sscanf(s.c_str(), "%d and %d are on the same level", &u, &v);
  47. return mp[u] -> level == mp[v] -> level;
  48. } else {
  49. return isFull;
  50. }
  51. }
  52. int main() {
  53. int n, k;
  54. cin >> n;
  55. post.resize(n);
  56. in.resize(n);
  57. for (int i = 0; i < n; i++) cin >> post[i];
  58. for (int i = 0; i < n; i++) cin >> in[i];
  59. root = create(0, n-1, 0, n-1, 1);
  60. cin >> k;
  61. getchar();
  62. for (int i = 0; i < k; i++) {
  63. string temp;
  64. getline(cin, temp);
  65. if (judge(temp)) printf("Yes\n");
  66. else printf("No\n");
  67. }
  68. return 0;
  69. }

发表评论

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

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

相关阅读