二叉树已知后序和中序求前序或层序;已知前序和中序,求后序或层序(C++详细代码)

目录

一、已知二叉树后序和中序

1、求前序:

2、求层序:

二、已知二叉树前序和中序

1、求后序:

2、求层序:


写出代码前,得先弄懂原理。如果你还没有弄懂类似:“给定二叉树已知后序序列和中序序列,或已知前序序列和中序序列,画出二叉树” 这类问题,建议先看一下此文:二叉树已知后序序列和中序序列,或已知前序序列和中序序列,画出二叉树(简单易懂

 

一、已知二叉树后序和中序

描述:

假设二叉树上各结点的权值互不相同且都为正整数

给定二叉树的后序遍历和中序遍历,请你输出二叉树的前序遍历和层序遍历

输入格式

第一行包含整数 N,表示二叉树结点总数。

第二行给出二叉树的后序遍历序列。

第三行给出二叉树的中序遍历序列。

1、求前序:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 111;

int n, postorder[maxn], inorder[maxn];
unordered_map<int, int> pos;

void build(int il, int ir, int pl, int pr){
	int root = postorder[pr];
	int k = pos[root];
	cout << root << " ";
	if(il < k) build(il, k - 1, pl, k - 1 - il + pl);
	if(ir > k) build(k + 1, ir, k - il + pl, pr - 1);
}

int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) cin >> postorder[i];
	for(int i = 0; i < n; i++){
		cin >> inorder[i];
		pos[inorder[i]] = i;
	}
	build(0, n - 1, 0, n - 1);
	
	return 0;
}

2、求层序:

#include<bits/stdc++.h>  //已知后中求层序
using namespace std;

const int maxn = 111;
int n, postorder[maxn], inorder[maxn];
unordered_map<int, int> l, r, pos;

int build(int il, int ir, int pl, int pr){
	int root = postorder[pr];
	int k = pos[root];
	if(il < k) l[root] = build(il, k - 1, pl, k - 1 - il + pl);
	if(ir > k) r[root] = build(k + 1, ir, k - il + pl, pr - 1);
	return root;
}

void bfs(int root){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int t = q.front();
		q.pop();
		cout << t << " ";
		if(l.count(t)) q.push(l[t]);
		if(r.count(t)) q.push(r[t]);
	}
}

int main(){
	cin >> n;
	for(int i = 0; i < n; i++)
		cin >> postorder[i];
	for(int i = 0; i < n; i++){
		cin >> inorder[i];
		pos[inorder[i]] = i;
	}
	int root = build(0, n - 1, 0, n - 1);
	bfs(root);
	
	return 0;
}

二、已知二叉树前序和中序

描述:

假设二叉树上各结点的权值互不相同且都为正整数

给定二叉树的前序遍历和中序遍历,请你输出二叉树的后序遍历和层序遍历

输入格式

第一行包含整数 N,表示二叉树结点总数。

第二行给出二叉树的前序遍历序列。

第三行给出二叉树的中序遍历序列。

1、求后序:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, preorder[maxn], inorder[maxn];
unordered_map<int, int> pos;

void build(int il, int ir, int pl, int pr){
	int root = preorder[pl];
	int k = pos[root];
	if(il < k) build(il, k - 1, pl + 1, k - 1 - il + pl + 1);
	if(ir > k) build(k + 1, ir, k - 1 - il + pl + 1 + 1, pr);
	cout << root << " ";
}

int main(){
	cin >> n;
	for(int i = 0; i < n; i++) cin >> preorder[i];
	for(int i = 0; i < n; i++){
		cin >> inorder[i];
		pos[inorder[i]] = i;
	}
	build(0, n - 1, 0, n - 1);	

	return 0;
}

2、求层序:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 100010;
int n, preorder[maxn], inorder[maxn];
unordered_map<int, int> l, r, pos; 

int build(int il, int ir, int pl, int pr){
	int root = preorder[pl];
	int k = pos[root];
	if(il < k) l[root] = build(il, k - 1, pl + 1, k - 1 - il + pl + 1);
	if(ir > k) r[root] = build(k + 1, ir, k - 1 - il + pl + 1 + 1, pr);
	return root;
}

void bfs(int root){
	queue<int> q;
	q.push(root);
	while(!q.empty()){
		int t = q.front();
		cout << t << " ";
		q.pop();
		if(l.count(t)) q.push(l[t]);
		if(r.count(t)) q.push(r[t]);
	}
}

int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) cin >> preorder[i];
	for(int i = 0; i < n; i++){
		cin >> inorder[i];
		pos[inorder[i]] = i;
	}
	int root = build(0, n - 1, 0, n - 1);
	bfs(root);
	
	return 0;
}

  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下为 C 语言二叉树已知前序中序后序的完整代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点 typedef struct node { char data; struct node *left; struct node *right; } TreeNode; // 递归构建二叉树 TreeNode* buildTree(char *preorder, char *inorder, int preStart, int preEnd, int inStart, int inEnd) { if (preStart > preEnd || inStart > inEnd) { return NULL; } // 获取前序遍历的根节点 TreeNode *root = (TreeNode*) malloc(sizeof(TreeNode)); root->data = preorder[preStart]; root->left = NULL; root->right = NULL; // 找到中序遍历中根节点的位置 int rootIndex; for (int i = inStart; i <= inEnd; i++) { if (inorder[i] == preorder[preStart]) { rootIndex = i; break; } } // 递归构建左子树和右子树 root->left = buildTree(preorder, inorder, preStart+1, preStart+rootIndex-inStart, inStart, rootIndex-1); root->right = buildTree(preorder, inorder, preStart+rootIndex-inStart+1, preEnd, rootIndex+1, inEnd); return root; } // 后序遍历输出二叉树 void postorderTraversal(TreeNode *root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%c", root->data); } int main(void) { char preorder[] = {'A', 'B', 'D', 'E', 'C', 'F'}; char inorder[] = {'D', 'B', 'E', 'A', 'F', 'C'}; TreeNode *root = buildTree(preorder, inorder, 0, 5, 0, 5); printf("后序遍历结果:"); postorderTraversal(root); printf("\n"); return 0; } ``` 以上代码实现了已知前序遍历和中序遍历构建二叉树,并输出后序遍历的结果。需要注意的是,代码中的前序遍历和中序遍历都是以字符数组的形式给出的,可以根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值