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.
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
解题思路:
先将题目中给出的中序遍历顺序,建立一棵树,树采用链表的方式,然后后序遍历这颗树并输出。
BuildTree函数说明:
题目中第一行给出这棵树的结点数是N,就可以知道接下来所有的语句数量是N*2,因为一个push对应一个pop;
在函数中建一个栈S用来保存结点(因为是中序遍历,右节点的插入需要知道父节点),建立一个father变量
用来记录当前结点,建立一个child变量用来表示,这次的push是要在左节点加,还是在右节点加。
代码:
#include <iostream>
#include <stack>
using namespace std;
#define Left_ 0
#define Right_ 1
using BinTree = class TreeNode*;
class TreeNode {
public:
int Data=-1;
BinTree Left=nullptr;
BinTree Right= nullptr;
};
BinTree BuidTree(); //用来接受中序遍历的语句,并且转换为树
void PostOrderTraversal(BinTree BT); //将树后序遍历输出
int main() {
BinTree BT=nullptr;
BT = BuidTree(); //建树
PostOrderTraversal(BT); //将建好的树后序遍历输出
return 0;
}
BinTree BuidTree() {
stack<BinTree> S; //存放结点
string s; //暂存语句
int x; //表示结点的数字
BinTree BT = nullptr; //树
BinTree father=nullptr; //表示父节点,nullptr代表根结点
int child = Left_; //用来表示孩子节点是右还是左
int N; //用来输入结点数,从而可以计算语句数
cin >> N;
for (int i = 0; i < N*2; i++) {
cin>>s;
if (s.length() == 4) { //语句是push x
cin >> x;
if (!father) {
BT = new TreeNode; //树
BT->Data = x; //如果是根节点就赋值
father = BT;//保存当前父节点
S.push(father);
}
else if (child == Left_) { //如果标记是左孩子就在左节点加
BinTree LN = new TreeNode;
LN->Data = x;
father->Left = LN;
father = LN;
S.push(father);
}
else if (child == Right_) { //如果标记是右孩子就在右节点加
BinTree RN = new TreeNode;
RN->Data = x;
father->Right = RN;
father = RN;
S.push(father);
child = Left_; //假如下一个语句是push就还在左节点加
}
}
else { //语句是pop
father = S.top(); //保存当前父节点
S.pop();
child = Right_; //遇到pop就更改child
}
}
return BT; //返回结点指针
}
void PostOrderTraversal(BinTree BT) { //后序遍历的递归算法
static int isFirst = 1; //用来判断是否是第一个结点
if (BT != nullptr) {
PostOrderTraversal(BT->Left);
PostOrderTraversal(BT->Right);
if (isFirst) {
cout << BT->Data; //输出隔一个空格,且结尾没有空格
isFirst = 0;
}
else cout << " " << BT->Data;
}
}
运行结果:
非递归的后序遍历函数:
void PostOrderTraversal_(BinTree BT) {
BinTree B = BT;
BinTree temp = nullptr; //暂时保存上次输出的树结点
stack<BinTree> S;
int isFirst = 1;
while (B || !S.empty()) {
while (B) {
S.push(B);
B = B->Left;
}
if (!S.empty()) {
B = S.top();
S.pop();
// 若右子树为空,或上次抛出的是右儿子,则可以抛出,temp不是NULL就是上次输出的右子树
if (B->Right == nullptr || B->Right == temp) {
if (isFirst) {
cout << B->Data;
isFirst = 0;
}
else {
cout << " " << B->Data;
}
temp = B; //更新上一次输出的树结点
B = nullptr; // 整个子树遍历完了,指针置为NULL,后续将弹出其父结点
}
//若当前结点仍有右子树,则将当前结点重新压入栈,将T指向其右子树
else {
S.push(B);
B = B->Right;
}
}
}
}