1. 程式人生 > >PAT A 1020. Tree Traversals (25) 由兩個遍歷序列得層次遍歷序列

PAT A 1020. Tree Traversals (25) 由兩個遍歷序列得層次遍歷序列

1020. Tree Traversals (25)

時間限制 400 ms
記憶體限制 65536 kB
程式碼長度限制 16000 B
判題程式 Standard 作者 CHEN, Yue

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
題意:本題就給你一個後序遍歷序列和中序遍歷序列,讓你得出該樹的層次遍歷序列
(這種情況我們必須要知道中序序列和其他某個序列才行)
思路:
對於這個題我們要得到它的層次遍歷序列,那麼我們就必須先得到這顆樹,然後才能得到層次遍歷序列
我們根據資料結構的相關知識,後序序列 “左右根” 中序序列“左根右” 先序序列“根左右”
所以後序遍歷的最後一個元素一定是整個樹的根,然後往前依次是右子樹根和左子樹根.,而中序遍歷我們先遍歷的是左子樹根然後是
根然後是右子樹根,所以我們現在後序遍歷中確定根,然後在中序遍歷中找到相應根的位置,進而就可以得到該根的左邊一定是左子樹
右面一定是右子樹,然後遞迴解決問題,建立好了樹,在進行層次遍歷即可
實現方法:
我們定義cur這個全域性遍歷,cur=n,設計兩個變數left和right,從後序序列的最後一個位置(樹的根)開始,
每次取出一個根,cur-1在中序遍歷中找到根,進而確定左右子樹,假設該根的位置為root,那麼根據中序遍歷,它的右子樹在
(root+1,right),左子樹在(left,root-1)..因為後序序列是左右根,所以根節點的前面是右子樹所以我們應該先遞迴
右子樹.當left==right說明為葉子節點.將T->left right 置空後返回..

得到整個樹以後再用佇列進行層次遍歷即可

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int post[33],in[33];
int n,cur;
typedef struct node *Tree;
struct node
{
  Tree left;
  Tree right;
  int num;
};
int findroot(int x)
{//在中序遍歷中確定根節點位置
  for(int i=1;i<=n;i++)
  {
    if(in[i]==x)
    {
      return i;
    }
  }
  return -1;
}
Tree createTree(int left,int right)
{//遞迴建立二叉樹
  if(left>right)
  return   NULL;
  int w=post[cur--];
  int root=findroot(w);
  Tree T=(Tree)malloc(sizeof(node));
  T->num=w;
  if(left==right)
  {
    T->left=NULL;
    T->right=NULL;
  }
  else
  {
    T->right=createTree(root+1,right);
    T->left=createTree(left,root-1);
  }
  return T;
}
void bfs(Tree T)
{//層次遍歷
  Tree q;
  queue<Tree>Q;
  Q.push(T);
  int flag=0;
  while(!Q.empty())
  {
    q=Q.front();
    Q.pop();
    if(flag!=0)
    printf(" ");
    printf("%d",q->num);
    if(q->left!=NULL)
    Q.push(q->left);
    if(q->right!=NULL)
    Q.push(q->right);
    flag=1; 
  }
}
int main()
{
    cin>>n;
    cur=n;
    for(int i=1;i<=n;i++)
    cin>>post[i];
    for(int i=1;i<=n;i++)
    cin>>in[i];
    Tree T=createTree(1,cur);
   bfs(T);
   return 0;
}

拓展:

我又寫了一個根據先序和中序遍歷獲得樹並進行層次遍歷的程式碼,方法還是一樣的。

根據先序遍歷的特點根左右我們就先從先序遍歷中確定根節點然後在中序遍歷中確定根節點的位置進而找到它的左右子樹

進行遞迴.

實現方法和上述的基本相同,不過這裡的cur我們應該從頭開始.然後每次+1,另外,因為先序遍歷先遍歷的根然後是左子

,然後是右子樹,所以我們要先遞迴左子樹,其餘的不再贅述

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+10;
int pre[maxn],in[maxn];
int cur,n;
typedef struct Tnode *Tree;
struct Tnode
{
	Tree left;
	Tree right;
	int num;
};
int findroot(int x)
{
	for(int i=1;i<=n;i++)
	{
		if(x==in[i])
		return i;
	}
	return -1;
}
Tree createTree(int left,int right)
{
	if(left>right) return NULL;
	int r=pre[cur++];
	int root=findroot(r);
	Tree T =(Tree)malloc(sizeof(Tnode));
	T->num=r;
	if(left==right)
	{
		T->left=NULL;
		T->right=NULL;
	}
	else
	{
		T->left=createTree(left,root-1);
		T->right=createTree(root+1,right);
	}
	return T;
}
void bfs(Tree T)
{
	Tree q;
	queue<Tree>Q;
	while(!Q.empty())
	Q.pop();
	Q.push(T);
	int flag=0;
	while(!Q.empty())
	{
		q=Q.front();
		Q.pop();
		if(flag!=0)
		printf(" ");
		flag=1;
		printf("%d",q->num);
		if(q->left!=NULL)
		Q.push(q->left);
		if(q->right!=NULL)
		Q.push(q->right);
	}
	return ;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>pre[i];
	for(int i=1;i<=n;i++)
	cin>>in[i];
	cur=1;
	Tree S=createTree(cur,n);
	bfs(S);
 	return 0;
}