1020. Tree Traversals (25)

深碍√TFBOYSˉ_ 2022-05-31 08:36 272阅读 0赞

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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 separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

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

Sample Output:

  1. 4 1 6 3 5 7 2

题目大意:

假设二叉树中所有的键值都是不同的正整数。给出后序遍历和中序遍历序列,你的任务是输出相应的层次遍历序列。
输入规格:
每个输入文件包含一个测试用例。对于每个测试用例,第一行给出一个正整数N(<=30),即二叉树中的节点总数。第二行给出后序遍历序列,第三行给出中序遍历序列。直线上得所有数字都用空格隔开。
输出规范:
对于那个测试用例,在一行中打印出相应的层次遍历序列。直线上得所有数字用一个空格隔开,在这一行的末尾没有多余的空格。

代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<queue>
  4. using namespace std;
  5. struct node
  6. {
  7. int data;
  8. struct node *lchild,*rchild;
  9. };
  10. struct node *create(int n,int *a,int *b)
  11. {
  12. struct node *root;
  13. int *p;
  14. if(n==0)
  15. return NULL;
  16. root=(struct node *)malloc(sizeof(struct node));
  17. root->data=a[n-1];
  18. for(p=b;p<p+n;p++)
  19. if(*p==a[n-1])
  20. break;
  21. int t;
  22. t=p-b;
  23. root->lchild=create(t,a,b);
  24. root->rchild=create(n-t-1,a+t,p+1);
  25. return root;
  26. }
  27. void level(struct node *T)
  28. {
  29. int i=0;
  30. struct node *p;
  31. queue<struct node *> que;
  32. que.push(T);
  33. while(!que.empty())
  34. {
  35. if(que.front())
  36. {
  37. p=que.front();
  38. que.push(p->lchild);
  39. que.push(p->rchild);
  40. if(i==0)
  41. {
  42. i++;
  43. printf("%d",p->data);
  44. }
  45. else
  46. {
  47. printf(" %d",p->data);
  48. }
  49. }
  50. que.pop();
  51. }
  52. }
  53. void cengci(struct node *root)
  54. {
  55. int out=0,in=0,i=0;
  56. struct node *q[100];
  57. q[in++]=root;
  58. while(in>out)
  59. {
  60. if(q[out])
  61. {
  62. if(i==0)
  63. {
  64. i++;
  65. printf("%d",q[out]->data);
  66. }
  67. else
  68. printf(" %d",q[out]->data);
  69. q[in++]=q[out]->lchild;
  70. q[in++]=q[out]->rchild;
  71. }
  72. out++;
  73. }
  74. }
  75. int main()
  76. {
  77. int i,j,n,m,k,t,a[31],b[31];
  78. struct node *root;
  79. scanf("%d",&n);
  80. for(i=0;i<n;i++)
  81. {
  82. scanf("%d",&a[i]);
  83. }
  84. for(i=0;i<n;i++)
  85. {
  86. scanf("%d",&b[i]);
  87. }
  88. root=create(n,a,b);
  89. level(root);
  90. //cengci(root);
  91. return 0;
  92. }

发表评论

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

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

相关阅读

    相关 1020 月饼 (25 分)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销

    相关 1020. 月饼 (25)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销

    相关 1020. 月饼 (25)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。 注意:销

    相关 A1020. Tree Traversals(25)

    这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列。 这题的核心: 1. 建树 2. BFS include<bits/