您的位置:首页 > 大数据 > 人工智能

1086. Tree Traversals Again (25)

2015-01-13 11:56 423 查看
#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
= {{}};
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: