Tree Traversals Again

题目地址http://www.patest.cn/contests/mooc-ds2015spring/03-%E6%A0%912

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 <string>
 3 #include <vector>
 4 using namespace std;
 5 typedef struct  TreeNode  *Bintree;
 6 struct  TreeNode{
 7     int Data;
 8     Bintree Left;
 9     Bintree Right;
10 };
11 int flag = 0;
12 void PreOrderTraversal(Bintree BT)
13 {
14     if (BT)
15     {
16         PreOrderTraversal(BT->Left);
17         PreOrderTraversal(BT->Right);
18         if (flag == 0)
19         {
20             cout << BT->Data;
21             flag = 1;
22         }
23         else 
24         cout << " "<<BT->Data;
25     }
26 }
27 TreeNode node[31];
28 
29 int main(){
30     int number;
31     cin >> number;
32     for (int i = 1; i <= number;i++)
33     {
34         node[i].Data = i;
35     }
36     int root;
37     string  opertion;
38     string  lastOpertion;
39     int  nodeNum = 0;
40     vector<int>  stacktree;
41     int lastNodeNum = 0;
42     cin >> opertion;
43     if (opertion == "Push")
44     {
45         cin >> nodeNum;
46         stacktree.push_back(nodeNum);
47     }
48     root = nodeNum;
49     lastOpertion = opertion;
50     for (int i = 0; i < 2 * number-1; i++)
51     {
52         cin >> opertion;
53         if (opertion == "Push")
54         {
55             cin >> nodeNum;
56             if (lastOpertion == "Push")
57             {
58                 node[stacktree[stacktree.size() - 1]].Left = &node[nodeNum];
59             }
60             else
61             {
62                 node[lastNodeNum].Right = &node[nodeNum];
63             }
64             stacktree.push_back(nodeNum);
65         }
66         if (opertion == "Pop")
67         {
68             lastNodeNum = stacktree[stacktree.size()-1];
69             stacktree.pop_back();
70         }
71         lastOpertion = opertion;
72     }
73     PreOrderTraversal(&node[root]);
74 
75     return 0;
76 }

 

posted @ 2015-04-06 13:44  lkyssd  阅读(160)  评论(0)    收藏  举报