码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2255 TreeRecovery(二叉树的三种遍历)

时间:2015-11-19 14:59:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

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: 
        D 
      /   \ 

B E
   /  \      \ 
  A     C     G 
            /
          F 

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).

 

题目解析:

题目好长,但实际上意思就是说给定一个二叉树的 preorder(先序遍历顺序:根,左子树, 右子树) 和 inorder  (中序遍历顺序:左子树, 根,右子树)

求二叉树的postorder(后续遍历顺序:左子树, 右子树, 根)

题目分析:

1)了解三种遍历方法后,知道先序遍历的第一个肯定是根节点,而中序遍历是先左,再根,再右,所以可以从中序遍历中找到第一个根节点。然后将其拆分两部分,左子树右子树。

再分别对这左子树和右子树进行遍历,找到根节点,再重复。整个下来,整个数就遍历了一遍。

2)输出后续遍历,因为在搜索遍历整颗树时,始终是对于中序遍历去找各个根节点,所以最先找到的,最后输出。

 

注:因为所有代码都不调用库函数,涉及到的库函数只有基本的输入输出,所以像strLen这样的基本函数也会重写一下。

代码如下:

#include <stdio.h>
#define MAX_N 27
char preOrder[MAX_N];
char inOrder[MAX_N];
int preIndex = 0;

int myStrLen(char str[]);
int search(int x, int y);
int main()
{
     //freopen("input.txt","r",stdin);
     while(scanf(" %s %s",&preOrder,&inOrder)==2)
     {
            preIndex = 0;
            search(0,myStrLen(preOrder)-1);
            printf("\n");
     }
     return 0;
}

int search(int x, int y)
{
      int i = 0;
      if(x>y)
         return 0;
      for(i=x;i<=y;i++)
     {
             if(inOrder[i] == preOrder[preIndex])
             {
                     break;
             }
     }
     preIndex++;
     search(x,i-1);
     search(i+1,y);
     printf("%c",inOrder[i]);
     return 0;
}

 

int myStrLen(char str[])
{
       int len = 0;
       while(*str!=‘\0‘)
       {
            len++;
            str++;
       }
       return len;
}

POJ2255 TreeRecovery(二叉树的三种遍历)

标签:

原文地址:http://www.cnblogs.com/xuxu-ning/p/4977529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!