牛客网:二叉树遍历【二叉树基础】

在这里插入图片描述
幸好提前发现了牛客网,一直玩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");
	}
}
发布了269 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/csyifanZhang/article/details/105611601