
幸好提前发现了牛客网,一直玩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");
}
}
本文分享了一次从ACM竞赛转向机试准备的经历,重点介绍了如何使用C++实现二叉树的构建与中序遍历。通过具体代码示例,详细解释了递归构建二叉树的方法及中序遍历的实现过程。

1051

被折叠的 条评论
为什么被折叠?



