C语言实现重建二叉树根据前序和中序遍历序列

理解:

在树相关的数据结构中经常利用递归思想,这是因为本身树的定义就是利用递归构成的。
举个例子:树的每一个子树又是一棵树,那么你前序遍历一棵树,就要从最小的子树开始,也就是说每一个子树的节点在序列中的相对位置,都是满足前序遍历的。
那么我们就可以利用递归来进行重建二叉树。

分析:

拥有的条件:1.二叉树前序序列。 2.二叉树的中序遍历。3.序列的长度。
     (1)根据前序序列,可以确定根节点数值,利用根节点数值和中序序列可以确定中序序列根节点的位置。
     (2)中序序列根节点的左边为左子树,右边为右子树,进而可以确定,左子树的个数,右子树的个数,中序序列中左右子树的范围位置。
     (3)前序序列中紧挨着根节点(前序序列的第一个位置)的就是左子树,根据上面得到的左子树的个数,可以确定前序序列中左右子树的范围位置。
根据(1)(2)(3)是不是又得到了左子树的前序,中序序列,以及序列的长度,也就是左子树的1,2,3. 同理还有右子树的1,2,3.
同理也就可以得到任意小子树的前序中序序列,而每一个节点的左右子树的根节点就是其左右子树的前序序列的第一个节点

那我们就只需做两件事:【1】将输入前序序列的第一个节点保存为根节点。
【2】根据当前序列,分出左右子树的前序中序序列,递归这两件事。

上核心代码:

binarytreeroot_ptr rebulit_binarytree(int *start_preorder, int *end_preorder, int *start_inorder, int * end_inorder){
   
	
	int lchild_length;
	binarytreeroot_ptr root;
	root = (binarytreeroot_ptr)malloc(sizeof(binarytreenode));
	root->data = *start_preorder;
	root->lchild = NULL;
	root->rchild = NULL;
	lchild_length = Find_location(start_preorder[0],start_inorder);
	printf("\t在中序当中的位置为:%d \n", lchild_length);
	if(start_preorder == end_preorder && start_inorder == end_inorder){
   
		if (*start_preorder == *start_inorder){
   
			return root;//叶子结点(只有自己,所有的序列都是自己) 
		}
	}
	if(lchild_length > 0) {
   
		root->lchild = rebulit_binarytree(start_preorder+1, start_preorder + lchild_length, start_inorder, start_inorder + lchild_length- 1);
	}
	if((start_inorder + lchild_length) < end_inorder){
   
		root->rchild = 
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是C语言实现前序中序遍历序列构建二叉树,并输出前序、中序、后序遍历结果的示例代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 根据前序中序遍历序列构建二叉树 TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) { if (preorderSize == 0 || inorderSize == 0) { return NULL; } // 创建根节点 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = preorder[0]; // 在中序遍历序列中找到根节点的位置 int rootIndex; for (rootIndex = 0; rootIndex < inorderSize; rootIndex++) { if (inorder[rootIndex] == root->val) { break; } } // 构建左子树 root->left = buildTree(preorder + 1, rootIndex, inorder, rootIndex); // 构建右子树 root->right = buildTree(preorder + rootIndex + 1, preorderSize - rootIndex - 1, inorder + rootIndex + 1, inorderSize - rootIndex - 1); return root; } // 前序遍历 void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } // 中序遍历 void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } // 后序遍历 void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } int main() { int preorder[] = {1, 2, 4, 5, 3, 6}; int inorder[] = {4, 2, 5, 1, 3, 6}; int n = sizeof(preorder) / sizeof(int); TreeNode* root = buildTree(preorder, n, inorder, n); printf("前序遍历结果:"); preorderTraversal(root); printf("\n"); printf("中序遍历结果:"); inorderTraversal(root); printf("\n"); printf("后序遍历结果:"); postorderTraversal(root); printf("\n"); return 0; } ``` 输出结果为: ``` 前序遍历结果:1 2 4 5 3 6 中序遍历结果:4 2 5 1 3 6 后序遍历结果:4 5 2 6 3 1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值