• 1159 Structure of a Binary Tree 甲级 xp_xht123


    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.
    • 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 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.

    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

    解题思路:首先建树(中序 , 后序)同时计算出每个节点的深度depth,并且存一下每个节点的value值和其节点的一一映射关系(题目保证不会重复,这样不需要每一次判断都要重新遍历整颗树)。

    下面都必须建立在这个节点存在的前提下

    第一点,求root:建树结束后返回的值就是root

    第二点,求siblings:层序遍历,是兄弟就是同根生,一个根的两个子树就是兄弟

    第三点,求parent,left child,right child:直接调用映射关系判断就行

    第四点,求level:调用每个节点的深度,进行判断就行

    第五点,求full tree:只要建树的时候没有只有右子树,没有左子树的情况出现,就是full tree

    最后一点:字符串处理,找到关键词就行,在一个字符串中出现上述的五点中的任意一点,用一对映射关系去存其type值就行,每一个串中的数字还需要截取出来,存入vector中,每一次有可能不同,所以需要clear

    1. /*
    2. 建树
    3. 第一点 找到根
    4. 第二点 每个点的深度
    5. */
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. using namespace std;
    13. const int N = 100;
    14. struct Node
    15. {
    16. int val;
    17. Node *right = NULL;
    18. Node *left = NULL;
    19. };
    20. bool flag = true;
    21. int n;
    22. int post[N] , in[N];
    23. unordered_map<int , int>mp;
    24. unordered_mapint>pan_sit;
    25. unordered_map<int , Node*>nod;
    26. int depth[1010] , bro[1010];
    27. /*
    28. A is the root 1
    29. A and B are siblings 2
    30. A is the parent of B 3
    31. A is the left child of B 4
    32. A is the right child of B 5
    33. A and B are on the same level 6
    34. It is a full tree 7
    35. */
    36. void init()
    37. {
    38. pan_sit["root"] = 1;
    39. pan_sit["siblings"] = 2;
    40. pan_sit["parent"] = 3;
    41. pan_sit["left"] = 4;
    42. pan_sit["right"] = 5;
    43. pan_sit["level"] = 6;
    44. pan_sit["full"] = 7;
    45. }
    46. Node* build(int il , int ir , int pl , int pr , int de)
    47. {
    48. Node* root = new Node;
    49. int k = mp[post[pr]];
    50. root -> val = in[k];
    51. depth[root -> val] = de;
    52. nod[root -> val] = root;
    53. if(il < k) root -> left = build(il , k - 1 , pl , pl + k - 1 - il , de + 1);
    54. if(ir > k) root -> right = build(k + 1 , ir , pr - ir + k , pr - 1 , de + 1);
    55. if(!root -> left && root -> right) flag = false;
    56. return root;
    57. }
    58. int k;
    59. vector<int>v;
    60. int pan(string s)
    61. {
    62. int idx = 0;
    63. for(int i = 0;i < s.length();i ++)
    64. {
    65. int j = i;
    66. while(j < s.length() && s[j] != ' ') j ++;
    67. string res = s.substr(i , j - i);
    68. //cout << res << " ";
    69. if(res[0] <= '9' && res[0] >= '0') v.push_back(stoi(res));
    70. if(pan_sit[res]) idx = pan_sit[res];
    71. i = j;
    72. }
    73. return idx;
    74. }
    75. bool bfs(Node *r)
    76. {
    77. queueq;
    78. q.push(r);
    79. while(!q.empty())
    80. {
    81. Node *t = q.front();
    82. q.pop();
    83. int cnt = 0;
    84. if(t -> left)
    85. {
    86. cnt ++;
    87. q.push(t -> left);
    88. }
    89. if(t -> right)
    90. {
    91. cnt ++;
    92. q.push(t -> right);
    93. }
    94. if(t -> right && t -> left)
    95. {
    96. bro[t -> right -> val] = t -> left -> val;
    97. bro[t -> left -> val] = t -> right -> val;
    98. }
    99. }
    100. }
    101. void judge(int type , Node *r)
    102. {
    103. if(type == 1)
    104. {
    105. if(v[0] == r -> val) puts("Yes");
    106. else puts("No");
    107. }
    108. else if(type == 2)
    109. {
    110. if(bro[v[0]] == v[1]) puts("Yes");
    111. else puts("No");
    112. }
    113. else if(type == 3)
    114. {
    115. if( (!nod[v[0]] -> left) || (!nod[v[0]] -> right))
    116. {
    117. puts("No");
    118. return;
    119. }
    120. if(nod[v[0]] -> left -> val == v[1]) puts("Yes");
    121. else if(nod[v[0]] -> right -> val == v[1]) puts("Yes");
    122. else puts("No");
    123. }
    124. else if(type == 4)
    125. {
    126. if(!nod[v[1]] -> left)
    127. {
    128. puts("No");
    129. return;
    130. }
    131. if(nod[v[1]] -> left -> val == v[0]) puts("Yes");
    132. else puts("No");
    133. }
    134. else if(type == 5)
    135. {
    136. if(!nod[v[1]] -> right)
    137. {
    138. puts("No");
    139. return;
    140. }
    141. if(nod[v[1]] -> right -> val == v[0]) puts("Yes");
    142. else puts("No");
    143. }
    144. else if(type == 6)
    145. {
    146. if(depth[v[0]] == depth[v[1]]) puts("Yes");
    147. else puts("No");
    148. }
    149. else
    150. {
    151. if(flag) puts("Yes");
    152. else puts("No");
    153. }
    154. }
    155. int main()
    156. {
    157. init();
    158. cin >> n;
    159. for(int i = 0;i < n;i ++) cin >> post[i];
    160. for(int i = 0;i < n;i ++)
    161. {
    162. cin >> in[i];
    163. mp[in[i]] = i;
    164. }
    165. Node* root = build(0 , n - 1 , 0 , n - 1 , 0);
    166. bfs(root);
    167. cin >> k;
    168. getchar();
    169. while(k --)
    170. {
    171. string s;
    172. getline(cin , s);
    173. int type = pan(s);
    174. judge(type , root);
    175. v.clear();
    176. }
    177. return 0;
    178. }

  • 相关阅读:
    CentOS Stream 9 设置
    mybatis日志、反射、DataSource
    JVM运行时数据区
    条码工具 Dynamic Web TWAIN HTML5 版本的工作原理
    一文搞懂 MySQL 索引数据结构
    HTTP请求、响应详解
    牛客周赛 Round 15
    【培训课程专用】ShareMemory的建立代码导读
    精品基于Javaweb的酒店民宿管理推荐平台SSM
    当社恐成为技术面试官
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126351900