PAT (Advanced Level) Practice 1086 Tree Traversals Again (25分) (寻找先序遍历(+建树+)后序遍历)
1.题目
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
2.题目分析
竖着看所有给出的数字,你会发现这些数字的序列正好是树的先序遍历,这样用栈模拟得到中序遍历,先序+中序(建树)求后序
3.代码
不建树:
#include<iostream>
#include<stack>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
int n;
bool space = false;
void out(vector<int>in, vector<int>pre, int root, int ins, int ine)
{
if (ins > ine)return ;
int i = ins;
while (in[i] != pre[root])i++;
if (i <= ine)
{
out (in, pre, root + 1, ins, i - 1);
out(in, pre, root + i + 1 - ins, i + 1, ine);
printf("%s%d", space == false ? "" : " ", pre[root]); space = true;
}
}
int main()
{
int a;
string temp;
scanf("%d", &n);
vector<int>in, pre;
stack<int>list;
while (1)
{
cin >> temp;
if (temp == "Push")
{
cin >> a;
pre.push_back(a);
list.push(a);
}
else
{
in.push_back(list.top());
list.pop();
}
if (in.size() == n&&list.size() == 0)break;
}
out(in, pre, 0, 0, n - 1);
}
建树:
#include<iostream>
#include<stack>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
typedef struct node *tree;
struct node
{
int data;
tree left;
tree right;
};
int n;
bool space = false;
void postordertraverse(tree t)
{
if (!t)return;
postordertraverse(t->left);
postordertraverse(t->right);
printf("%s%d", space == false ? "" : " ", t->data); space = true;
}
tree create(tree t,vector<int>in,vector<int>pre,int root,int ins,int ine)
{
if (ins > ine)return NULL;
if (!t)
{
t = (tree)malloc(sizeof(struct node));
t->left = t->right = NULL;
t->data = pre[root];
}
int i = ins;
while (in[i] != pre[root])i++;
if (i <= ine)
{
t->left=create(t->left,in, pre, root + 1, ins, i - 1);
t->right = create(t->right, in, pre, root + i +1 - ins, i + 1, ine);
}
return t;
}
int main()
{
int a;
string temp;
scanf("%d", &n);
vector<int>in,pre;
stack<int>list;
while (1)
{
cin >> temp;
if (temp == "Push")
{
cin >> a;
pre.push_back(a);
list.push(a);
}
else
{
in.push_back(list.top());
list.pop();
}
if (in.size() == n&&list.size() == 0)break;
}
tree t = NULL;
t=create(t, in, pre, 0, 0, n - 1);
postordertraverse(t);
}