博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 2255 Tree Recovery
阅读量:6429 次
发布时间:2019-06-23

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

1.Link:

2.Content:

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11448   Accepted: 7186

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 ABCDEFGBCAD CBAD

Sample Output

ACBFGEDCDAB

Source

3.Method:

4.Code:

1 #include "stdio.h" 2 #include "string.h" 3 #define NUM 27 4 char ans[NUM]; 5 int pos; 6 void func(char a[],char b[],int al,int bl,int len) 7 { 8     int i; 9     if(len==0) return;10     else if(len==1) ans[pos--]=a[al];11     else12     {13         ans[pos--]=a[al];14         for(i=bl;i

 

5.Reference

转载于:https://www.cnblogs.com/mobileliker/p/3933405.html

你可能感兴趣的文章
Intel 揭秘:如何在公有云、混合云和私有云间合理放置工作负载
查看>>
借力AI 极验如何构建下一代业务安全?
查看>>
用Python制作迷宫GIF
查看>>
支付宝推出基于区块链跨境支付,巨头入场小企业将面临灭顶之灾
查看>>
从事互联网行业,怎样才能快速掌握一门编程语言呢?
查看>>
谈谈fail-fast与fail-safe是什么以及工作机制
查看>>
深入浅出换肤相关技术以及如何实现
查看>>
Redis 基础、高级特性与性能调优
查看>>
React native 第三方组件 React native swiper
查看>>
接口幂等设计
查看>>
编程入门指南
查看>>
移动端的自适应方案—REM
查看>>
你真的懂volatile吗
查看>>
Android 编译时注解-提升
查看>>
说说 Spring AOP 中 @Aspect 的高级用法
查看>>
Workbox CLI中文版
查看>>
贝聊亿级数据库分库分表实践
查看>>
同时连接gitlab和github
查看>>
vuex源码分析
查看>>
香港智远:港股光伏板块中报期有望获资金青睐
查看>>