已知二叉树先序遍历中序遍历求其后序遍历、重建二叉树

本文详细介绍了如何通过已知的二叉树先序遍历和中序遍历序列,重建二叉树,并进一步获取其后序遍历序列的过程。通过递归算法实现,文章提供了C++代码实例,帮助读者理解从遍历序列到二叉树结构的转换。重建二叉树的步骤清晰地展示了二叉树遍历在数据结构中的应用,对于学习数据结构和算法的读者具有较高的实践价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

已知二叉树先序遍历中序遍历求其后序遍历

      (注:已知中序遍历序列和剩下两种遍历序列中的一种都可以确定二叉树,即可得到另一种遍历序列,      但是已知前序遍历和后序遍历序列并不能唯一确定二叉树,例如:preorder:AB postorder:BA,我们不能确定B是A的左子还是右子。)

      二叉树的先序遍历的第一个元素总是树的根结点,而在中序遍历中,根结点在序列的中间,其左右两边分别是根结点的左右子树。

      因此我们可以根据先序遍历确定根结点,然后在中序遍历中扫描根结点,同时可得到左右子树的中序遍历和先序遍历,递归此过程,即可获得二叉树的所有结点。

      而后序遍历即为根结点左子树的后序遍历+右子树的后序遍历+根结点。

     递归实现思路非常简单,C++代码:

 

string getpostorder(char* preorder, char* inorder, int length){
	string str = "";
	char* rootInorder;        //根结点在中序遍历中的位置
	int leftlength;           //左子树长度
	if (length <= 0||preorder==NULL||inorder==NULL)//递归边界条件
		return "";
	else{
		leftlength = 0;
		rootInorder = inorder;
                //扫描根结点
                while (leftlength < length&& *rootInorder != preorder[0]){
			leftlength++;
			rootInorder++;
		}
		if (rootInorder == inorder+length-1 && *rootInorder != preorder[0])
			throw exception("Invalid input.");
                //递归
                str = getpostorder(preorder + 1,inorder,leftlength)+getpostorder(preorder+leftlength+1,inorder+leftlength+1,length-leftlength-1);
		str.push_back(preorder[0]);
		return str;
	}
}

 

 

已知二叉树先序遍历中序遍历重建二叉树

       二叉树的重建,也是运用递归方法思想,首先建立根结点,然后递归构建出其左右子树,递归边界条件为先序遍历和中序遍历只有一个元素的时候。

       二叉树结点一般结构为:

typedef struct BinaryTreeNode{
    char data;
    struct BinaryTreeNode *left;
    struct BinaryTreeNode *right;
}BiTreeNode;

 

重建代码:

 

 

BinaryTreeNode *Construct(char* preorder,char* inorder, int length){
	if (preorder == NULL || inorder == NULL || length <= 0)
		return NULL;
	return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}

//递归函数 前两个参数和后两个参数分别确定了某子树先序遍历和中序遍历序列
//参数为指针,操作方便
BinaryTreeNode *ConstructCore(char* startPreorder, char* endPreorder, char* startInorder, char* endInorder){
        //构建根节点
        char rootValue = startPreorder[0];
	BinaryTreeNode* root = new BinaryTreeNode();
	root->left = root->right = NULL;
	root->data = rootValue;
	if (startPreorder == endPreorder){
		if (startInorder == endInorder && *startPreorder == *startInorder){
			return root;
		}
		else
			throw exception("Invalid input.");
	}
	
	char* rootInorder = startInorder;
        //扫描中序遍历寻找根结点
	while (rootInorder <= endInorder && *rootInorder != *startPreorder)
		++rootInorder;
	if (rootInorder==endInorder && *rootInorder!=rootValue)
		throw exception("Invalid input.");

        //这两个变量用于确定左右子树
	int leftLength = rootInorder - startInorder;
	char* leftPreorderEnd = startPreorder + leftLength;

	if (leftLength > 0){
                //构建左子树
		root->left = ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
	}
	if (leftLength < endPreorder - startPreorder){//右子树
		root->right = ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
	}
	return root;
}

 

 

 

加上main()函数之后的完整代码:

#include<iostream>
#include<string>
#include<cstring>
#include<stack>
using namespace std;

typedef struct BinaryTreeNode{
	char data;
	struct BinaryTreeNode *left;
	struct BinaryTreeNode *right;
}BiTreeNode;

BinaryTreeNode *ConstructCore(char* startPreorder, char* endPreorder, char* startInorder, char* endInorder){
	char rootValue = startPreorder[0];
	BinaryTreeNode* root = new BinaryTreeNode();
	root->left = root->right = NULL;
	root->data = rootValue;
	if (startPreorder == endPreorder){
		if (startInorder == endInorder && *startPreorder == *startInorder){
			return root;
		}
		else
			throw exception("Invalid input.");
	}
	
	char* rootInorder = startInorder;
	while (rootInorder <= endInorder && *rootInorder != *startPreorder)
		++rootInorder;
	if (rootInorder==endInorder && *rootInorder!=rootValue)
		throw exception("Invalid input.");

	int leftLength = rootInorder - startInorder;
	char* leftPreorderEnd = startPreorder + leftLength;

	if (leftLength > 0){
		root->left = ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
	}
	if (leftLength < endPreorder - startPreorder){
		root->right = ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
	}
	return root;
}

BinaryTreeNode *Construct(char* preorder,char* inorder, int length){
	if (preorder == NULL || inorder == NULL || length <= 0)
		return NULL;
	return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}

//递归遍历重建的二叉树
string posttravel(BinaryTreeNode* root){
	string str="";
	if (root != NULL){
		str = posttravel(root->left) + posttravel(root->right);
		str.push_back(root->data);
		return str;
	}
	else
		return "";
}

string getpostorder(char* preorder, char* inorder, int length){
	string str = "";
	char* rootInorder;
	int leftlength;
	if (length <= 0||preorder==NULL||inorder==NULL)
		return "";
	else{
		leftlength = 0;
		rootInorder = inorder;
		while (leftlength < length&& *rootInorder != preorder[0]){
			leftlength++;
			rootInorder++;
		}
		if (rootInorder == inorder+length-1 && *rootInorder != preorder[0])
			throw exception("Invalid input.");
		str = getpostorder(preorder + 1,inorder,leftlength)+getpostorder(preorder+leftlength+1,inorder+leftlength+1,length-leftlength-1);
		str.push_back(preorder[0]);
		return str;
	}
}

int main(){
	string postorder;
	char preorder[100], inorder[100];
	cout << "Input preoder & inorder of the binary tree:";
	cin >> preorder >> inorder;
	BinaryTreeNode* root=Construct(preorder,inorder,strlen(preorder));
	//postorder = posttravel(root);//此函数为递归后序遍历重建的二叉树
	postorder = getpostorder(preorder, inorder, strlen(preorder));
	cout << postorder << endl;
	system("pause");
	return 0;
}

 

 

 

 

 

 

 

在C语言中,给定一棵已知二叉树序遍历(根节点 -> 左子树 -> 右子树)和中序遍历(左子树 -> 根节点 -> 右子树),我们可以通过递归的方式得后序遍历(左子树 -> 右子树 -> 根节点)。这是因为前序、中序和后序遍历之间存在一定的关联: 1. **后序遍历**的根节点在最后,所以我们可以通过以下步骤找到它: - 当遍历到当前节点时,如果它是序遍历的第一个元素,那么它就是根节点。 - 接着,我们在中序遍历中查找该节点的位置。由于中序遍历根节点位于左右子树之间,所以我们可以找到从当前开始的剩余部分,这部分就是中序遍历剩下的左子树和右子树。 - 对这个剩余部分分别进行后序遍历即可得到完整的后序遍历序列。 下面是递归实现的伪代码示例: ```c struct TreeNode *findRoot(struct TreeNode *root, int preorder[], int size) { // 序遍历第一个元素即为根节点 if (preorder[0] == root->val) return root; // 中序遍历找到根节点的位置 for (int i = 1; i < size; i++) { if (preorder[i] == root->val) { return findRoot(root->left, inorder, size); } } } void postorderTraversal(struct TreeNode *root, int inorder[], int size) { if (root == NULL) return; postorderTraversal(root->left, inorder, size); postorderTraversal(root->right, inorder, size); // 将找到的根节点添加到后序序列的末尾 insertAtEnd(postorder, root->val); // 假设insertAtEnd()是一个函数用于将值追加到数组末尾 } ``` 这里假设`inorder[]`数组保存了中序遍历的结果,并且`postorder[]`数组用于存储最终的后序遍历结果。你需要根据实际情况调整这些操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值