题目
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
解答
本题的关键思路在于想到样例输入时,push进的顺序时前序遍历的顺序,pop出来的顺序是中序遍历的顺序,由这两个序列,我们可以复原出二叉树。然后对二叉树后序遍历输出即可得到答案。
关于如何通过前序遍历和中序遍历还原二叉树的方法可以看我的这篇文章https://blog.csdn.net/Nino_sama/article/details/100548533
import java.util.Stack;
import java.util.ArrayList;
import java.io.*;
public class Main {
static ArrayList<Integer> postEle = new ArrayList<>();
public static void main(String[] args) throws IOException {
Main main = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
ArrayList<Node> preNodes = new ArrayList<>();
ArrayList<Node> inNodes = new ArrayList<>();
Stack<Node> stack = new Stack<>();
for (int i = 0; i < num * 2; i++) {
String str = br.readLine();
String[] strArr = str.split(" ");
if (strArr[0].equals("Push")) {
Node node = new Node(Integer.parseInt(strArr[1]));
stack.push(node);
preNodes.add(node);
} else {
inNodes.add(stack.pop());
}
}
Node root = getRoot(preNodes, 0, preNodes.size() - 1, inNodes, 0, inNodes.size() - 1);
main.postEle(root);
for (int i = 0; i < postEle.size(); i++) {
System.out.print(postEle.get(i));
if (i != postEle.size() - 1) {
System.out.print(" ");
}
}
}
public void postEle(Node root) {
if (root == null) {
return;
}
postEle(root.left);
postEle(root.right);
this.postEle.add(root.element);
}
static Node getRoot(ArrayList<Node> preNodes, int begin1, int end1, ArrayList<Node> inNodes, int begin2, int end2) {
if (begin1 > end1 || begin2 > end2) {
return null;
}
Node root = preNodes.get(begin1);
for (int i = begin2; i <= end2; i++) {
if (root == inNodes.get(i)) {
root.left = getRoot(preNodes, begin1 + 1, i + begin1 - begin2, inNodes, begin2, i - 1);
root.right = getRoot(preNodes, i + begin1 - begin2 + 1, end1, inNodes, i + 1, end2);
}
}
return root;
}
static class Node {
public int element;
public Node left;
public Node right;
public Node(int element) {
this.element = element;
left = null;
right = null;
}
}
}