博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2255 Tree Recovery【二叉树重建】
阅读量:7022 次
发布时间:2019-06-28

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

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

Sample Input

DBACEGF ABCDEFG

BCAD CBAD

Sample Output

ACBFGED

CDAB

code:

View Code
#include
#include
struct node {
int left; int right; }tr[100]; int root; char pr[100]; char mi[100]; void init(int p) {
tr[p].left=-1; tr[p].right=-1; } void fason(int pa,int son) {
if(son

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/16/2400036.html

你可能感兴趣的文章
hadoop-eclipse插件的使用
查看>>
日常问题
查看>>
ajax只运行一次的问题
查看>>
nginx的location和rewrite
查看>>
笔试算法题(18):常数时间删除节点 & 找到仅出现一次的两个数字
查看>>
compareTo
查看>>
day040 数据库索引补充 存储过程 事务等
查看>>
indexzero/http-server-1-简介
查看>>
Javascript文件的动态下载
查看>>
Spring logger 配置
查看>>
oracle分析函数
查看>>
Webstrom 快捷键大全
查看>>
查询的所有操作快速处理整合数组
查看>>
C语言程序
查看>>
R in Action 读书笔记(6)基本图形
查看>>
linux+node.js+redis+mongodb+nginx环境的搭建
查看>>
制作个人网页
查看>>
iphone-common-codes-ccteam源代码 CCUIImage.m
查看>>
ssh简单配置
查看>>
第五章4
查看>>