1020 Tree Traversals (25 point(s))

本文介绍了一种算法,该算法可以根据二叉树的中序和后序遍历序列,重建二叉树并输出层次遍历序列。通过递归创建树结构,并利用队列进行层次遍历,实现了从给定序列到层次遍历序列的转换。

1020 Tree Traversals (25 point(s))

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

根据中序遍历和后序遍历得出层次遍历。 

关键点:

1. 重建树,体会createTree()函数;

2. 采用队列的层次遍历。

注意点:

1. C++11引入nullptr作为空指针。C++11以下支持NULL(视为0)。尽量使用nullptr。

2. createTree函数注意终止条件(即某子树不存在,left>right,返回空指针)。

#include<iostream>
#include<queue>
using namespace std;
int post[30];
int in[30];
struct Node{
	int data;
	Node* left;
	Node* right; 
	Node(int d):data(d),left(NULL),right(NULL){ }
};  
Node* createTree(int root,int left,int right){
	//根据post[]的root位置的元素和in[]的left到right区间的元素构建树 
	if(left>right) return NULL;
	Node* r = new Node(post[root]);
	int idx = left;
	while(in[idx]!=post[root]){
		idx++;
	}//idx是在中序遍历中根节点的位置 
	r -> left = createTree(root-right+idx-1,left,idx-1);
	r -> right = createTree(root-1,idx+1,right);
	return r;//必须返回当前的根节点 
}
void levelOrder(Node* root){
	queue<Node*> q;
	q.push(root);
	bool isFirst=true;
	while(!q.empty()){
		Node* front = q.front();
		q.pop();
		if(isFirst){
			cout<<front->data;
			isFirst = false;
		}
		else cout<<" "<<front->data;
		if(front->left!=NULL) q.push(front->left);
		if(front->right!=NULL) q.push(front->right);
	}
}
int main(void){
	int n;cin>>n;
	for(int i=0;i<n;i++) cin>>post[i];
	for(int i=0;i<n;i++) cin>>in[i];
	Node* root = createTree(n-1,0,n-1);
	levelOrder(root);
	return 0;
}

 

【CNN-GRU-Attention】基于卷积神经网络和门控循环单元网络结合注意力机制的多变量回归预测研究(Matlab代码实现)内容概要:本文介绍了基于卷积神经网络(CNN)、门控循环单元网络(GRU)与注意力机制(Attention)相结合的多变量回归预测模型研究,重点利用Matlab实现该深度学习模型的构建与仿真。该模型通过CNN提取输入数据的局部特征,利用GRU捕捉时间序列的长期依赖关系,并引入注意力机制增强关键时间步的权重,从而提升多变量时间序列回归预测的精度与鲁棒性。文中涵盖了模型架构设计、训练流程、参数调优及实际案例验证,适用于复杂非线性系统的预测任务。; 适合人群:具备一定机器学习与深度学习基础,熟悉Matlab编程环境,从事科研或工程应用的研究生、科研人员及算法工程师,尤其适合关注时间序列预测、能源预测、智能优化等方向的技术人员。; 使用场景及目标:①应用于风电功率预测、负荷预测、交通流量预测等多变量时间序列回归任务;②帮助读者掌握CNN-GRU-Attention混合模型的设计思路与Matlab实现方法;③为学术研究、毕业论文或项目开发提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码逐模块理解模型实现细节,重点关注数据预处理、网络结构搭建与注意力机制的嵌入方式,并通过调整超参数和更换数据集进行实验验证,以深化对模型性能影响因素的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值