幸好提前发现了牛客网,一直玩ACM,打线段树,主席树,树状数组打得我都要吐了,结果一回头发现机试达不到这么高的要求吐血,然而自己连基本的二叉树都不熟练。简单复习一下二叉树的构建与遍历
#include<iostream>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define ll int
#define MAX 105
#define inf 0x3fffffff
struct node {
char s;
node *lc, *rc;
node(char a = ' ') { s = a, lc = NULL, rc = NULL; }
};
ll i = 0;
node * build(string s) {
node * t = new node(s[i++]);
if (t->s == '#') return t;
t->lc = build(s);
t->rc = build(s);
return t;
}
void pprint(node * r) {
if (r->s == '#')return;
pprint(r->lc);
printf("%c ", r->s);
pprint(r->rc);
}
int main() {
string s;
while (cin >> s) {
i = 0;
node * root = build(s);
pprint(root);
printf("\n");
}
}