pat a1020 后序+中序,建树,并求层序序列

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 

给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn=50;

struct Node
{
	int data;
	Node* lchild;
	Node* rchild;
};

int pre[maxn],in[maxn],post[maxn];  //全局变量
int n;

Node* create(int postL,int postR,int inL,int inR)
{
	if(postL>postR) return NULL;
	
	Node* root=new Node;
	root->data=post[postR];
	int k;
	for(k=inL;k<=inR;k++)
	{
		if(in[k]==post[postR]) break;
	}
	int numLeft=k-inL;
	root->lchild=create(postL,postL+numLeft-1,inL,k-1);
	root->rchild=create(postL+numLeft,postR-1,k+1,inR);
	
	return root;
}

int num=0;

void BFS(Node* root)
{
	queue<Node*> q;
	q.push(root);
	while(!q.empty())
	{
		Node* now=q.front();
		q.pop();
		printf("%d",now->data);
		num++;
		if(num<n) printf(" ");
		if(now->lchild!=NULL) q.push(now->lchild);
		if(now->rchild!=NULL) q.push(now->rchild);
	}
} 

int main()
{
	scanf("%d",&n);
	
	for(int i=0;i<n;i++)
	{
		scanf("%d",&post[i]);
	}
	for(int i=0;i<n;i++)
	{
		scanf("%d",&in[i]);
	}
	Node* root=create(0,n-1,0,n-1);
	BFS(root);
	return 0;
}

 

要使用C++根据后序遍历和中遍历遍历,可以按照以下步骤实现: 1. 首先,根据后序遍历和中遍历构建二叉树。后序遍历的最后一个元素是根节点,在中遍历中找到该根节点的位置,将中遍历分为左子树和右子树的部分,同时也能确定后序遍历中左子树和右子树的部分,然后递归构建左右子树。 2. 然后,使用队列进行遍历,将根节点入队,之后不断从队列中取出节点,访问该节点,将其左右子节点入队,直到队列为空。 以下是实现代码: ```cpp #include <iostream> #include <vector> #include <queue> using namespace std; // 定义二叉树节点结构 struct TreeNode { char val; TreeNode* left; TreeNode* right; TreeNode(char x) : val(x), left(nullptr), right(nullptr) {} }; // 根据中遍历和后序遍历构建二叉树 TreeNode* buildTree(vector<char>& inorder, vector<char>& postorder) { if (inorder.empty() || postorder.empty()) return nullptr; char rootVal = postorder.back(); TreeNode* root = new TreeNode(rootVal); int rootIndex = 0; for (int i = 0; i < inorder.size(); ++i) { if (inorder[i] == rootVal) { rootIndex = i; break; } } vector<char> leftInorder(inorder.begin(), inorder.begin() + rootIndex); vector<char> rightInorder(inorder.begin() + rootIndex + 1, inorder.end()); vector<char> leftPostorder(postorder.begin(), postorder.begin() + rootIndex); vector<char> rightPostorder(postorder.begin() + rootIndex, postorder.end() - 1); root->left = buildTree(leftInorder, leftPostorder); root->right = buildTree(rightInorder, rightPostorder); return root; } // 遍历二叉树 vector<char> levelOrder(TreeNode* root) { vector<char> result; if (!root) return result; queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); result.push_back(node->val); if (node->left) q.push(node->left); if (node->right) q.push(node->right); } return result; } int main() { vector<char> inorder = {'D', 'B', 'E', 'A', 'C'}; vector<char> postorder = {'D', 'E', 'B', 'C', 'A'}; TreeNode* root = buildTree(inorder, postorder); vector<char> levelTraversal = levelOrder(root); for (char c : levelTraversal) { cout << c << " "; } cout << endl; return 0; } ``` ### 代码解释: 1. **TreeNode结构体**:定义了二叉树的节点结构,包含节点的值`val`,以及左右子节点指针`left`和`right`。 2. **buildTree函数**:根据中遍历和后序遍历构建二叉树。首先取后序遍历的最后一个元素作为根节点,在中遍历中找到该根节点的位置,将中遍历和后序遍历分别划分为左右子树的部分,然后递归构建左右子树。 3. **levelOrder函数**:使用队列进行遍历。将根节点入队,然后不断从队列中取出节点,访问该节点将其左右子节点入队,直到队列为空。 4. **main函数**:定义了中遍历和后序遍历的序列,调用`buildTree`函数构建二叉树,再调用`levelOrder`函数进行遍历,输出遍历的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值