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;
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值