码迷,mamicode.com
首页 > 其他好文 > 详细

1086. Tree Traversals Again (25)

时间:2015-12-06 12:59:53      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

时间限制
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

 

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





1086. Tree Traversals Again (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!