//03-树3 Tree Traversals Again
import java.util.Scanner;
import java.util.Stack;
class node{
int number = 0;
int left = 30;
int right = 30;
}
public class Main {
final static int Max_Size = 30;
static int count = 0;
static node[] Read(Scanner s) {
Stack<Integer> stack = new Stack<Integer>();
String prec, curc;
int n = s.nextInt();
node[] T = new node[Max_Size + 1];
int x = 0;
int f = 1;
prec = s.next();
int top = 0;
x = s.nextInt();
T[0] = new node();
T[0].number = x;
top ++;
stack.push(0);
if(n == 1) {
curc = s.next();
}
for(int i = 0; i < 2 * n -2; i ++) {
curc = s.next();
if(curc.equals("Push")) {
x = s.nextInt();
if(prec.equals("Push")) {
T[f] = new node();
T[f - 1].left = f;
T[f].number = x;
stack.push(f);
f ++;
top ++;
}
else if(prec.equals("Pop")) {
T[f] = new node();
T[f].number = x;
T[top].right = f;
stack.push(f);
f ++;
top ++;
}
prec = "Push";
}
if(curc.equals("Pop")) {
top = stack.pop();
prec = "Pop";
}
}
return T;
}
static void PostOrderTraversal(node N, node[] T) {
int n = 0;
while(T[n] != null) {
n ++;
}
if (N != null)
{
PostOrderTraversal(T[N.left], T);
PostOrderTraversal(T[N.right], T);
System.out.print(N.number);
count ++;
if(count < n) {
System.out.print(" ");
}
}
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
node[] T = Read(s);
s.close();
//System.out.println();
PostOrderTraversal(T[0], T);
}
}
03-树3 Tree Traversals Again
最新推荐文章于 2022-05-05 14:23:18 发布