博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2255 TreeRecovery(二叉树的三种遍历)
阅读量:5303 次
发布时间:2019-06-14

本文共 2548 字,大约阅读时间需要 8 分钟。

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;
}

转载于:https://www.cnblogs.com/xuxu-ning/p/4977529.html

你可能感兴趣的文章
Effect-Compiler Tool(fxc.exe)
查看>>
django中的缓存 单页面缓存,局部缓存,全站缓存 跨域问题的解决
查看>>
常见HTTP状态码(200、301、302、500等)
查看>>
Atiti.大企业病与小企业病 大公司病与小公司病
查看>>
处理器管理与进程调度
查看>>
解决随机数生成的坐标在对角线上的问题
查看>>
服务器ganglia安装
查看>>
HashMap的存储结构及原理
查看>>
在线即时展现 Html、JS、CSS 编辑工具 - JSFiddle
查看>>
veridata实验例(3)验证veridata发现insert操作不会导致同步
查看>>
django数据库交互
查看>>
【转载】SQL注入攻防入门详解
查看>>
图说二叉树添加数据原理以及遍历原理
查看>>
NTC(负温度)热敏电阻.阻值的计算方式
查看>>
ps aux 状态介绍
查看>>
二级指针内存模型
查看>>
bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割
查看>>
【Scala】Scala之Packaging and Imports
查看>>
【译】Java编程动态性,第 2部分: 反射简介
查看>>
png8、16、24、32位的区别
查看>>