问题
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
解答
#include <iostream>
#include <stack>
#include <string>
using namespace std;
template <typename T>
class Node
{
public:
T data;
Node<T> *left;
Node<T> *right;
Node(int d = 0, Node<T> *p = nullptr, Node<T> *q = nullptr)
{
data = d;
left = p;
right = q;
}
};
template <typename T>
class Tree
{
public:
unsigned N;
Node<T> *root;
Tree(int n) : N(n)
{
if (n == 0)
{
root = nullptr;
return;
}
string op;
int d;
stack<Node<T> *> s;
Node<T> *t = nullptr;
for (int i = 0; i < 2 * n; ++i)
{
cin >> op;
if (op == "Push")
{
cin >> d;
Node<T> *p = new Node<T>(d);
if (i == 0)
root = p;
if (t and !t->right)
t->right = p;
else if (!s.empty())
s.top()->left = p;
s.push(p);
}
else if (op == "Pop")
{
t = s.top();
s.pop();
}
else
exit(-1);
}
}
void postOrder(Node<T> *p)
{
if (!p)
return;
postOrder(p->left);
postOrder(p->right);
if (p != root)
cout << p->data << " ";
}
~Tree()
{
;
}
};
int main()
{
int n = 0;
cin >> n;
Tree<unsigned> t1(n);
t1.postOrder(t1.root);
cout << t1.root->data;
cout << endl;
return 0;
}
#include <iostream>
#include <string>
#include <stack>
/*
using namespace std;
template <typename T>
class Node
{
public:
T data;
Node<T> *left;
Node<T> *right;
Node(int d = 0, Node<T> *p = nullptr, Node<T> *q = nullptr)
{
data = d;
left = p;
right = q;
}
};
template <typename T>
class Tree
{
public:
unsigned N;
Node<T> *root;
Tree(int n) : N(n)
{
if (n == 0)
{
root = nullptr;
return;
}
string op;
int d;
stack<Node<T> *> s;
Node<T> *t = nullptr;
for (int i = 0; i < 2 * n; ++i)
{
cin >> op;
if (op == "Push")
{
cin >> d;
Node<T> *p = new Node<T>(d);
if (i == 0)
root = p;
if (t and !t->right)
t->right = p;
else if (!s.empty())
s.top()->left = p;
s.push(p);
}
else if (op == "Pop")
{
t = s.top();
s.pop();
}
else
exit(-1);
}
}
void postOrder(Node<T> *p)
{
if (!p)
return;
postOrder(p->left);
postOrder(p->right);
if (p != root)
cout << p->data << " ";
}
~Tree()
{
;
}
};
int main()
{
int n = 0;
cin >> n;
Tree<unsigned> t1(n);
t1.postOrder(t1.root);
cout << t1.root->data;
cout << endl;
return 0;
}
*/
class Solution
{
private:
int _n;
int *preorder;
int *inorder;
public:
int *postorder;
Solution(int n, const int *pre, const int *in) : _n(n)
{
preorder = new int[n];
inorder = new int[n];
postorder = new int[n];
for (int i = 0; i < n; ++i)
{
preorder[i] = pre[i];
inorder[i] = in[i];
}
}
void solve(int preL, int inL, int postL, int n)
{
if (n == 0) return;
if (n == 1)
{
postorder[postL] = preorder[preL];
return;
}
int root = preorder[preL];
postorder[postL + n - 1] = root;
int i = 0;
for (; i < n; ++i)
if (inorder[inL + i] == root)
break;
solve(preL + 1, inL, postL, i);
solve(preL + i + 1, inL + i + 1, postL + i, n - i - 1);
}
void print()
{
int i = 0;
for(;i<_n-1;++i)
std::cout<<postorder[i]<<' ';
std::cout<<postorder[i]<<'\n';
}
~Solution()
{
delete[]preorder;
delete[]inorder;
delete[]postorder;
}
};
int main()
{
int n = 0, d= 0;
std::string op;
std::stack<int> s;
std::cin>>n;
int *pre = new int[n];
int *in = new int[n];
for (int i = 0,j=0,k=0; i < 2 * n; ++i)
{
std::cin >> op;
if (op == "Push")
{
std::cin >> d;
pre[j++]=d;
s.push(d);
}
else
{
d = s.top();
in[k++] = d;
s.pop();
}
}
Solution S(n, pre, in);
S.solve(0, 0, 0, n);
S.print();
delete []pre;
delete []in;
return 0;
}