版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mobius_strip/article/details/82821786
题目
已知一颗BST的前根序序列,输出它的后根续序列。
分析
数据结构。按照输入的顺序建立BST,并使用递归输出即可。
说明
开始把output函数的返回值写成int,然后已知RE,编译器优化问题?
#include <cstdlib>
#include <cstdio>
const int data_size = 10001;
//bst__bgein
typedef struct _tnode
{
_tnode* Lchild;
_tnode* Rchild;
int Value;
}tnode;
tnode Node[data_size];
int bst_count = 0;
void bst_insert(tnode* &r, int value)
{
if (!r) {
r = &Node[bst_count ++];
r->Value = value;
r->Lchild = NULL;
r->Rchild = NULL;
} else if (value < r->Value) {
bst_insert(r->Lchild, value);
} else {
bst_insert(r->Rchild, value);
}
}
void bst_post(tnode *r)
{
if (r) {
bst_post(r->Lchild);
bst_post(r->Rchild);
printf("%d\n", r->Value);
}
}
//bst__end
int data[data_size];
int main()
{
int size = 0;
while (~scanf("%d", &data[size])) {
size ++;
}
if (size) {
tnode *bst_root = NULL;
for (int i = 0; i < size; ++ i) {
bst_insert(bst_root, data[i]);
}
bst_post(bst_root);
}
return 0;
}