#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node {
int key;
node *left, *right;
};
struct command {
int type;
int key;
};
node * create(command cmd[], int &index, int n) {
node *t = NULL;
if(index < n) {
command &c = cmd[index];
if(c.type == 0) {
t = new node;
t->key = c.key;
index ++; // consume push
t->left = create(cmd, index, n);
index ++; // consume pop
t->right = create(cmd, index, n);
}
}
return t;
}
void postorder(node *t) {
if(t) {
postorder(t->left);
postorder(t->right);
printf("%d ", t->key);
}
}
#define N 60
int main(int argc, char **argv) {
command cmd[N] = {{}};
int n;
cin >> n;
for(int i = 0; i < 2*n; i ++) {
char str[10];
scanf("%s", str);
if(!strcmp(str, "Push")) {
cmd[i].type = 0;
scanf("%d", &cmd[i].key);
}
else {
cmd[i].type = 1;
}
}
int index = 0;
node *t = create(cmd, index, 2*n);
postorder(t->left);
postorder(t->right);
printf("%d", t->key);
return 0;
}
1086. Tree Traversals Again (25)
最新推荐文章于 2019-09-05 10:59:57 发布