【编程之美】3.9已知先序和中序遍历,求后序遍历+POJ-2255

很久以前写过,现在把我的递归版本代码贴出

struct tree
{
	char c;
	struct tree * left_child;
	struct tree * right_child;
} * root;
tree * create_tree(char pre[], char in[], int len)
{
	if (len == 0) return NULL;
	tree * t = new tree;
	t->c = pre[0];
	int i;
	for (i = 0; i < len; ++i)
		if (in[i] == pre[0])
			break;
	int j;
	t->left_child = create_tree(pre + 1, in, i);
	t->right_child = create_tree(pre + i + 1, in + i + 1, len - i - 1);
	return t;
}
void print(tree * t) {
	if (t == NULL) return ;
	print(t->left_child);	
	print(t->right_child);
	cout<<t->c;
}
int main()
{
	char pre[30], in[30];
	root = new tree;
	while (scanf("%s%s", pre, in) == 2) {
		root = create_tree(pre, in, strlen(pre));
		print(root);
		printf("\n");
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值