HDU--1710--Binary Tree Traversals(二叉樹遍歷)

Description

A binary tree isa finite set of vertices that is either empty or consists of a root r and twodisjoint binary trees called the left and right subtrees. There are three mostimportant ways in which the vertices of a binary tree can be systematicallytraversed or ordered. They are preorder, inorder and postorder. Let T be abinary tree with root r and subtrees T1,T2. 

In a preorder traversal of the vertices of T, we visit the root r followed byvisiting the vertices of T1 in preorder, then the vertices of T2 in preorder. 

In an inorder traversal of the vertices of T, we visit the vertices of T1 ininorder, then the root r, followed by the vertices of T2 in inorder. 

In a postorder traversal of the vertices of T, we visit the vertices of T1 inpostorder, then the vertices of T2 in postorder and finally we visit r. 

Now you are given the preorder sequence and inorder sequence of a certainbinary tree. Try to find out its postorder sequence. 

 

Input

The inputcontains several test cases. The first line of each test case contains a singleinteger n (1<=n<=1000), the number of vertices of the binary tree.Followed by two lines, respectively indicating the preorder sequence andinorder sequence. You can assume they are always correspond to a exclusivebinary tree. 

 

Output

For each testcase print a single line specifying the corresponding postorder sequence. 

 

Sample Input

 9

1 2 4 7 3 5 8 96

4 7 2 1 8 5 9 36

 

Sample Output

 7 4 2 8 9 5 6 3 1

 

題意:給你一個前序遍歷和中序遍歷,要求後序。

模板.......

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1005
using namespace std;
int pre[N],in[N],post[N];        //存放先序,中序,後序的數組 
int n;
struct Node{
	int data;
	Node* lchild;
	Node* rchild;
};
Node* create(int prel,int prer,int inl,int inr)  //根據先序和中序建立樹 
{           //4個參數  先序的左右邊界,中序的左右邊界  
	if(prel>prer)                              //已經遍歷完了,返回 
	{
		return NULL;
	}
	Node* root=new Node;                      //建立一個根結點,用來存放當前的樹 
	root->data=pre[prel];                     // 因爲是已知先序,所以當前樹的值,一定等於先序的最左邊的點 
	int k;                                    //記錄當前根節點在中序的位置 
	for(k=inl;k<inr;k++)                     
	{
		if(in[k]==pre[prel])                 //找到位置,跳出 
		break;
	}
	int numleft=k-inl;                        //當前樹的左子樹的大小 
	root->lchild=create(prel+1,prel+numleft,inl,k-1);     //將這部分存入左子樹 
	root->rchild=create(prel+numleft+1,prer,k+1,inr);     // 將這部分存入右子樹
	return root;                //返回根結點的地址 
}
int num=0;                        //控制最後一個輸出沒有空格 
void printfpost(Node* root)               //後序輸出 
{
	if(root==NULL)
	return;
	printfpost(root->lchild);
	printfpost(root->rchild);
	printf("%d",root->data);
	num++;
	if(num<n) printf(" ");
}
int main()
{
	while(~scanf("%d",&n))
	{
		num=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&pre[i]);
		}
		for(int i=0;i<n;i++)
		{
			scanf("%d",&in[i]);
		}
		Node* root=create(0,n-1,0,n-1);
		printfpost(root);
		printf("\n");
	}
}


發佈了64 篇原創文章 · 獲贊 28 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章