先序、中序、后序序列的二叉树构造算法

1、定义

        如果同时知道二叉树的先序序列和中序序列,或者同时知道二叉树的后序序列和中序序列,就能确定这颗二叉树的形状。

2、应用

        通过二叉树的先序序列、中序序列或后序序列、中序序列,可构造出唯一的二叉树结构。

3、实现

(1)二叉树的类型定义

typedef char ElemType;

typedef struct BTNode
{
	ElemType data;
	BTNode *left, *right;
}BTNode;

(2)利用先序序列和中序序列构造二叉树

BTNode* CreateBTByPreAndIn(ElemType* pre, ElemType* in, int n)
{
	if (n <= 0) return NULL;
	BTNode* b = new BTNode;
	b->data = *pre;
	int k;
	for (ElemType* p = in; p < in + n; p++)
	{
		if (*p == *pre)
		{
			k = p - in;
			break;
		}
	}
	b->left = CreateBTByPreAndIn(pre + 1, in, k);
	b->right = CreateBTByPreAndIn(pre + 1 + k, in + 1 + k, n - k - 1);
	return b;
}

(4)利用后序序列和中序序列构造二叉树

BTNode* CreateBTByPostAndIn(ElemType* post, ElemType* in, int n)
{
	if (n <= 0) return NULL;
	BTNode* b = new BTNode;
	b->data = *(post + n - 1);
	int k;
	for (ElemType* p = in; p < in + n; p++)
	{
		if (*p == *(post + n - 1))
		{
			k = p - in;
			break;
		}
	}
	b->left = CreateBTByPostAndIn(post, in, k);
	b->right = CreateBTByPostAndIn(post + k, in + k + 1, n - k - 1);
	return b;
}
4、测试
#include <iostream>
#include <queue>
using namespace std;

typedef char ElemType;

typedef struct BTNode
{
	ElemType data;
	BTNode *left, *right;
}BTNode;

void LevelOrder(BTNode* root)
{
	queue<BTNode*> q;
	if (root)
	{
		q.push(root);
		while (!q.empty())
		{
			BTNode* t = q.front();
			cout << t->data << ", ";
			if (t->left) q.push(t->left);
			if (t->right) q.push(t->right);
			q.pop();
		}
	}
	cout << endl;
}

BTNode* CreateBTByPreAndIn(ElemType* pre, ElemType* in, int n)
{
	if (n <= 0) return NULL;
	BTNode* b = new BTNode;
	b->data = *pre;
	int k;
	for (ElemType* p = in; p < in + n; p++)
	{
		if (*p == *pre)
		{
			k = p - in;
			break;
		}
	}
	b->left = CreateBTByPreAndIn(pre + 1, in, k);
	b->right = CreateBTByPreAndIn(pre + 1 + k, in + 1 + k, n - k - 1);
	return b;
}

BTNode* CreateBTByPostAndIn(ElemType* post, ElemType* in, int n)
{
	if (n <= 0) return NULL;
	BTNode* b = new BTNode;
	b->data = *(post + n - 1);
	int k;
	for (ElemType* p = in; p < in + n; p++)
	{
		if (*p == *(post + n - 1))
		{
			k = p - in;
			break;
		}
	}
	b->left = CreateBTByPostAndIn(post, in, k);
	b->right = CreateBTByPostAndIn(post + k, in + k + 1, n - k - 1);
	return b;
}

int main()
{

	const char* pre = "ABDGCEF";
	const char* in = "DGBAECF";
	const char* post = "GDBEFCA";
	LevelOrder(CreateBTByPreAndIn(const_cast<ElemType*>(pre), 
		const_cast<ElemType*>(in), strlen(pre)));
	LevelOrder(CreateBTByPostAndIn(const_cast<ElemType*>(post),
		const_cast<ElemType*>(in), strlen(post)));
	system("pause");
	return 0;
}



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这道题需要用到二叉树先序遍历、中序遍历、后序遍历的特点,以及递归的思想。具体步骤如下: 1. 根据先序序列找到根节点,即A; 2. 在中序序列中找到A所在的位置,将中序序列分为左子树序列和右子树序列,即DGB和ECF; 3. 根据左子树序列DGB和右子树序列ECF,分别构造左子树和右子树; 4. 对左子树和右子树分别递归执行以上步骤,直到所有节点都被构造完成。 具体实现代码如下: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): if not preorder or not inorder: return None # 根据先序序列找到根节点 root = TreeNode(preorder[0]) # 在中序序列中找到根节点的位置 idx = inorder.index(preorder[0]) # 构造左子树 root.left = buildTree(preorder[1:idx+1], inorder[:idx]) # 构造右子树 root.right = buildTree(preorder[idx+1:], inorder[idx+1:]) return root def postorderTraversal(root): if not root: return [] # 递归遍历左子树 left = postorderTraversal(root.left) # 递归遍历右子树 right = postorderTraversal(root.right) # 合并左右子树的遍历结果 return left + right + [root.val] # 测试 preorder = ['A', 'B', 'D', 'G', 'C', 'E', 'F'] inorder = ['D', 'G', 'B', 'A', 'E', 'C', 'F'] root = buildTree(preorder, inorder) postorder = postorderTraversal(root) print(postorder) ``` 运行结果为:['G', 'D', 'B', 'E', 'F', 'C', 'A'],与题目要求的后序序列GDBEFCA一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值