`
java-mans
  • 浏览: 11344328 次
文章分类
社区版块
存档分类
最新评论

Tree Recovery---POJ--2255

 
阅读更多

Tree Recovery

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 3771

Accepted: 2526

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:


To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!

Input

The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.

Output

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG

BCAD CBAD

Sample Output

ACBFGED

CDAB

Source

Ulm Local 1997

先读懂题意:

很简洁,就是已知二叉树的前序遍历和中序遍历序列,要求求出后序遍历序列。

基本知识:

1)前序遍历:先访问当前节点,然后以前序访问左子树、右子树;

2)中序遍历:先以中序遍历左子树,接着访问当前节点,最后以中序遍历右子树;

3)后序遍历:先以后序遍历左子树,接着以后序遍历右子树,最后访问当前节点;

4)规律:前序遍历的每一个节点,都是当前子树的根节点;同时,以对应的节点为边界,就会把中序遍历的结果分成左子树和右子树;

解题思路:

肯定使用递归啦!编写递归函数preInToPost(...),该函数先访问前序遍历序列的当前节点,然后根据该节点值在中序遍历序列中找到所在的位置index,以index作为杠杆点,将中序遍历序列分为左右两个数组,分别作为左子树和右子树,接着依次对左子树、右子树递归调用,最后输出根节点。(后序遍历嘛!)

代码如下:

#include <iostream>

#include <string>

//在字符数组A中位置startend之间查找字符c的坐标

int find(char c, char* Arr, int start , int end)

{

int index = -1;

for(index=start; index<=end; index++)

{

if(c == Arr[index])

return index;

}

}

void preInToPost(char *preOrder, int preStart, int preEnd,

char *inOrder, int inStart, int inEnd)

{

char c; //存储当前前序序列的第一个字符

int index; //前序序列第一个字符在中序序列中的下标

//递归退出的边界条件

if(inStart > inEnd)

return;

//当前中序序列只剩一个字符,也就是说访问到叶子节点了

//对该节点的后序遍历就是输出直接该节点(该节点左右子树都没了)

if(inStart == inEnd)

{

std::cout<<inOrder[inStart];

return;

}

c = preOrder[preStart]; //取出当前前序序列的第一个字符

index = find(c, inOrder, inStart, inEnd); //找出前序序列第一个字符在中序序列中的下标

//index为杠杆点,将inOrder分为左右两个数组,这两个数组就是相应的左子树和右子树了

//而后序遍历就是依次后序遍历左子树、右子树,最后才访问当前根节点

//index-inStart是左子树的节点个数

preInToPost(preOrder, preStart+1, preStart+(index-inStart),

inOrder, inStart, index-1); //对左子树递归

preInToPost(preOrder, preStart+(index-inStart)+1, preEnd,

inOrder, index+1, inEnd); //对右子树递归

//最后输出根节点(后序遍历嘛)

std::cout<<c;

}

int main()

{

std::string pPreOrder;

std::string pInOrder;

while(std::cin>>pPreOrder>>pInOrder)

{

preInToPost(const_cast<char*>(pPreOrder.c_str()), 0, pPreOrder.length()-1,

const_cast<char*>(pInOrder.c_str()), 0, pInOrder.length()-1);

std::cout<<std::endl;

}

system("pause");

return 0;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics