题目
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
运行结果
Case | Hint | Result | Run Time | Memory |
---|---|---|---|---|
0 | sample 有单边有双边结点 | Accepted | 3 ms | 512 KB |
1 | 单边喇叭张开形 | Accepted | 2 ms | 424 KB |
2 | 交错 | Accepted | 2 ms | 384 KB |
3 | N=1 | Accepted | 3 ms | 512 KB |
4 | N=30,复杂组合 | Accepted | 2 ms | 360 KB |
程序
#include<iostream>
using namespace std;
#define Tree int
#define ElementType int
#define MaxNodes 30
#define Null 0
struct Node{
ElementType Element=Null;
Tree Left = Null;
Tree Right = Null;
}BT[MaxNodes];
typedef struct Node *PtrToNode;
//测试用例-中序遍历思路
//Tree MiddleOrderBuildTree_Test(struct Node BT[]);
Tree MiddleOrderBuildTree(struct Node BT[]);
int *PostOrderTraversals(struct Node BT[], int root);
int main()
{
int root;
int *Results;
/*
//测试用例-中序遍历思路
root = MiddleOrderBuildTree_Test(BT);
*/
root = MiddleOrderBuildTree(BT);
//后序遍历思路
Results = PostOrderTraversals(BT, root);
//输出结果个数
int NResults;
for(NResults=0; NResults<MaxNodes; NResults++)
if(Results[NResults] == 0) break;
//安装输出格式
for(int i=0; i< NResults-1; i++)
cout << Results[i] << " ";
cout << Results[NResults-1] <<endl;
return 0;
}
int *PostOrderTraversals(struct Node BT[], int root)
{
static int Results[MaxNodes];
int Results_i = 0;
int stack_[MaxNodes];
int stack_i = -1;//栈顶下标
int CurrData;
PtrToNode CurrNode;//当前指向的结点
int pre = Null;
for(int i=0; i<MaxNodes; i++) Results[i] = 0;
CurrNode = &BT[root];
while(CurrNode->Element >0 || stack_i>=0){
while(CurrNode->Element > 0){
//压入当前元素到堆栈
++stack_i;
stack_[stack_i] = CurrNode->Element;
CurrNode = &BT[CurrNode->Left];
}
if(stack_i >= 0){
//取出栈顶元素
CurrData = stack_[stack_i];
--stack_i;
CurrNode = &BT[CurrData];
//向回走
if((CurrNode->Right == Null) || CurrNode->Right == pre){
Results[Results_i] = CurrData;
++Results_i;
pre = CurrNode->Element;
CurrNode = &BT[0];
}
//向下走
else{
++stack_i;
stack_[stack_i] = CurrNode->Element;
CurrNode = &BT[CurrNode->Right];
}
}
}
return Results;
}
Tree MiddleOrderBuildTree(struct Node BT[])
{
int N;
int root = Null;
int stack_[MaxNodes];
int stack_i = -1;//栈顶下标
int CurrData = Null;
PtrToNode CurrNode;//当前指向的结点
cin >> N;
string operations[2*N];
int InputData[2*N];
//输入格式要求
for(int i=0; i<2*N; i++){
cin >> operations[i];
if(operations[i] == "Push")
cin >> InputData[i];
else if(operations[i] == "Pop")
InputData[i] = 0;
}
//取出当前操作数值
CurrData = InputData[0];
//创建根结点
BT[CurrData].Element = CurrData;
//将第一个push值压入堆栈
++stack_i;
stack_[stack_i] = CurrData;
//指定根结点
root = CurrData;
CurrNode = &BT[CurrData];
for(int i=1; i<2*N; i++){
if(operations[i] == "Push"){
//读取当前操作数值
CurrData = InputData[i];
//将新的操作数值压入堆栈
++stack_i;
stack_[stack_i] = CurrData;
//左孩子为空,则优先填补
if(CurrNode->Left == Null)
CurrNode->Left = CurrData;
else
CurrNode->Right = CurrData;
//创建新结点
BT[CurrData].Element = CurrData;
CurrNode = &BT[CurrData];
}
else if(operations[i] == "Pop"){
//将栈顶元素取出
CurrData = stack_[stack_i];
--stack_i;
//指向当前结点
CurrNode = &BT[CurrData];
}
}
return root;
}
Tree MiddleOrderBuildTree_Test(struct Node BT[])
{
static int N = 6;
int root = Null;
int stack_[MaxNodes];
int stack_i = -1;//栈顶下标
int CurrData = Null;
PtrToNode CurrNode;//当前指向的结点
string operations[2*N] = \
{"Push", "Push", "Push", "Pop", "Pop", "Push", "Pop", "Pop", \
"Push", "Push", "Pop", "Pop"};
int InputData[2*N] = {1,2,3,0,0,4,0,0,5,6,0,0};
//取出当前操作数值
CurrData = InputData[0];
//创建根结点
BT[CurrData].Element = CurrData;
//将第一个push值压入堆栈
++stack_i;
stack_[stack_i] = CurrData;
//指定根结点
root = CurrData;
CurrNode = &BT[CurrData];
for(int i=1; i<2*N; i++){
if(operations[i] == "Push"){
//读取当前操作数值
CurrData = InputData[i];
//将新的操作数值压入堆栈
++stack_i;
stack_[stack_i] = CurrData;
//左孩子为空,则优先填补
if(CurrNode->Left == Null)
CurrNode->Left = CurrData;
else
CurrNode->Right = CurrData;
//创建新结点
BT[CurrData].Element = CurrData;
CurrNode = &BT[CurrData];
}
else if(operations[i] == "Pop"){
//将栈顶元素取出
CurrData = stack_[stack_i];
--stack_i;
//指向当前结点
CurrNode = &BT[CurrData];
}
}
return root;
}