1086. Tree Traversals Again (25)

时间限制

200 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

push 的顺序就是二叉树的前序

pop的顺序就是二叉树的中序遍历

本质上还是考根据这两个顺序建立二叉树,并且进行后序遍历

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1

来源: <http://www.patest.cn/contests/pat-a-practise/1086>

  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #pragma warning(disable:4996)
  5. using namespace std;
  6. vector<int> s;
  7. int inOrder[32] = { 0 },preOrder[32] = { 0 };
  8. int n;
  9. struct Node {
  10. int val;
  11. Node* left = NULL;
  12. Node* right = NULL;
  13. };
  14. Node* BuildTree(int* pre, int* in, int n) {
  15. if (n <= 0)
  16. return NULL;
  17. int i;
  18. for (i = 0; i < n; i++) {
  19. if (*(in + i) == *pre) {
  20. break;
  21. }
  22. }
  23. Node* p = (Node*)malloc(sizeof(Node));
  24. p->val = *pre;
  25. p->left = BuildTree(pre + 1, in, i);
  26. p->right = BuildTree(pre + i + 1, in + i + 1, n - i - 1);
  27. return p;
  28. }
  29. void PostOrder(Node *p) {
  30. if (p->left != NULL)
  31. PostOrder(p->left);
  32. if (p->right != NULL)
  33. PostOrder(p->right);
  34. if (p->val != preOrder[0])
  35. cout << p->val << " ";
  36. else
  37. cout << p->val;
  38. }
  39. int main(void) {
  40. freopen("Text.txt", "r", stdin);
  41. cin >> n;
  42. string str;
  43. for (int i = 0; i < 2 * n; i++) {
  44. cin >> str;
  45. if (str == "Push") {
  46. int temp;
  47. cin >> temp;
  48. for (int j = 0; j < 31; j++) {
  49. if (preOrder[j] == 0) {
  50. preOrder[j] = temp;
  51. break;
  52. }
  53. }
  54. s.push_back(temp);
  55. }
  56. else {
  57. for (int j = 0; j < 31; j++) {
  58. if (inOrder[j] == 0) {
  59. inOrder[j] = s[s.size() - 1];
  60. break;
  61. }
  62. }
  63. s.pop_back();
  64. }
  65. }
  66. Node* root = (Node*)malloc(sizeof(Node));
  67. root = BuildTree(preOrder, inOrder, n);
  68. PostOrder(root);
  69. return 0;
  70. }

来自为知笔记(Wiz)

时间: 2024-12-13 03:08:23

1086. Tree Traversals Again (25)的相关文章

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to

PAT 1086 Tree Traversals Again (25)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop()

PAT Advanced 1086 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop()

PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); po

PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h> #include<string.h> #include<stack> using namespace std; const int MAX=50; int n,cnt=0; //n个节点,cnt在后序输出的时候控制空格数量用 int PRE[MAX],IN[MAX]; //先序,中序 int preI,inI; //先序,中序的下标 stack<int> S; //栈 struct node { int data; nod

PAT (Advanced Level) 1086. Tree Traversals Again (25)

入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<stack> using namespace std; const int maxn=1000+10; const int INF=0x7FFFFFFF; int n,tot; int Preorder[maxn],Inorder[maxn],Po

pat1086. Tree Traversals Again (25)

1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with th

03-树3. Tree Traversals Again (25)

03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with th

03-3. Tree Traversals Again (25)

03-3. Tree Traversals Again (25) An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: