【PAT】1020. Tree Traversals (25)

本文介绍了一种根据后序和中序遍历序列重建二叉树,并进行层次遍历的方法。通过递归构建二叉树节点,最终实现从根到叶子的层次输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


分析:考察树的建立和遍历。

可参考《编程之美》3.9


#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std;

struct Node{
	Node *left;
	Node *right;
	int value;
	Node():left(NULL),right(NULL){}
};

void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){
	//判断何时结束递归
	if(PostOrder == NULL || InOrder == NULL)
	{
		root = NULL;
		return ;
	}
	if(root == NULL) root = new Node;
	root->value = *(PostOrder + len - 1);
	root->left = NULL;
	root->right = NULL;
	if(len == 1) 
		return;

	int count = 0;
	int *temp = InOrder;
	while(*temp != *(PostOrder + len -1))
	{
		count ++;
		temp++;
		if(count > len) break;
	}
	int left = temp - InOrder ;
	int right = len - left - 1;
	if(left > 0)
		Rebuild(PostOrder, InOrder, left, root->left);
	if(right > 0)
		Rebuild(PostOrder + left, InOrder+left+1, right, root->right);
}


int main()
{
	int n,i,t;
	while(cin>>n)
	{
		int *PostOrder = new int[n];
		int *InOrder = new int[n];
		for(i=0; i<n; i++)
			cin>>PostOrder[i];
		for(i=0; i<n; i++)
			cin>>InOrder[i];

		Node *root = new Node;
		int post_start,in_start;
		post_start = 0;
		in_start = 0;
		Rebuild(PostOrder, InOrder, n, root);
		queue<Node *> q;
		q.push(root);
		int flag = 1;

		while(!q.empty()){
			if(q.front()->left != NULL)
				q.push(q.front()->left);
			if(q.front()->right != NULL)
				q.push(q.front()->right);
			if(flag != n)
				cout<<q.front()->value<<" ";
			else 
				cout<<q.front()->value;
			flag ++;
			q.pop();
		}
		
		cout<<endl;
	}
	return 0;
}


今天自己写了一个版本:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int *postOrder;
int *inOrder;

struct node{
	node(){
		left = NULL;
		right = NULL;
	}
	int val;
	node *left;
	node *right;
};

node* build(int *postOrder, int *inOrder, int len, node*root){
	int val = postOrder[len-1];
	root->val = val;
	int i, leftLen=0;
	for(i=0; i<len; i++){
		leftLen++;
		if(inOrder[i] == val){
			break;
		}
	}		
	if(leftLen-1>0){
		node *left = new node();
		left = build(postOrder, inOrder, leftLen-1, left);
		root->left = left;
	}else{
		root->left = NULL;
	}
	int rightLen = len-leftLen;
	if(rightLen>0){
		node *right = new node();
		right = build(postOrder+(leftLen-1), inOrder+leftLen, rightLen, right);
		root->right = right;
	}else{
		root->right = NULL;
	}	
	return root;	
}

int main(int argc, char** argv) {
	int n;
	scanf("%d",&n);
	postOrder = new int[n];
	inOrder = new int[n];
	int i;
	for(i=0; i<n; i++){
		scanf("%d",&postOrder[i]);
	}
	for(i=0; i<n; i++){
		scanf("%d",&inOrder[i]);
	}
	
	node *root = new node();
	root = build(postOrder,inOrder,n,root);
	queue<node> que;
	que.push(*root);
	vector<int> vec;
	while(!que.empty()){
		node tmp = que.front();
		vec.push_back(tmp.val);
		if(tmp.left!=NULL){
			que.push(*tmp.left);
		}
		if(tmp.right!=NULL){
			que.push(*tmp.right);
		}
		que.pop();
	}
	for(i=0; i<vec.size(); i++){
		if(i==0){
			printf("%d",vec[i]);
		}else{
			printf(" %d",vec[i]);
		}
	}
	printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值