描述

给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度≤8)。

格式

输入格式

第一行为二叉树的中序序列

第二行为二叉树的后序序列

输出格式

一行,为二叉树的先序序列

样例1

样例输入1

BADC

BDCA

样例输出1

ABCD

限制

每个测试点1s

来源

noip2001普及组第三题

<br/ >

<br/ >

解析:根据中序序列和后续序列构造出这棵二叉树,再先序遍历即可。

#include <iostream>
#include <string>
using namespace std; string s1, s2; struct Node{
char val;
Node *l , *r;
Node(){
l = NULL;
r = NULL;
}
}; Node* build(const string& s1, const string& s2)
{
Node *root = new Node();
root->val = *(s2.end()-1);
size_t pos = s1.find(root->val);
if(pos != 0){
string left_s1 = s1.substr(0, pos);
string left_s2 = s2.substr(0, pos);
root->l = build(left_s1, left_s2);
}
if(pos != s1.length()-1){
string right_s1 = s1.substr(pos+1);
string right_s2 = s2.substr(pos, s2.length()-1-pos);
root->r = build(right_s1, right_s2);
}
return root;
} void pre_order(Node *root)
{
if(root != NULL){
cout<<root->val;
pre_order(root->l);
pre_order(root->r);
}
} void destroy(Node *root)
{
if(root->l != NULL)
destroy(root->l);
if(root->r != NULL)
destroy(root->r);
delete root;
} int main()
{
cin>>s1>>s2;
Node *root = build(s1, s2);
pre_order(root);
destroy(root);
return 0;
}

Vijos 1132 求二叉树的先序序列的更多相关文章

  1. 【递归】Vijos P1132 求二叉树的先序序列(NOIP2001普及组第三题)

    题目链接: https://vijos.org/p/1132 题目大意: 给定二叉树的中序和后序遍历,求该二叉树先序遍历. 题目思路: [递归] 这题妥妥递归. 二叉树先序根左右,中序左根右,后序左右 ...

  2. Vijos1132:求二叉树的先序序列

    描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 格式 输入格式 第一行为二叉树的中序序列第二行为二叉树的后序序列 输出格式 一行,为二叉树的先序 ...

  3. hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java ...

  4. SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

    求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description  已知一 ...

  5. SDUTOJ 1489 求二叉树的先序遍历

    <img src="http://img.blog.csdn.net/20141014212549703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  6. 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)

    Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...

  7. 分别求二叉树前、中、后序的第k个节点

    一.求二叉树的前序遍历中的第k个节点 //求先序遍历中的第k个节点的值 ; elemType preNode(BTNode *root,int k){ if(root==NULL) return ' ...

  8. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  9. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

随机推荐

  1. javascript加速运动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. SDUT 1220 完美数

    完美数 Time Limit: 1000ms   Memory limit: 65536K 题目描述 任何一个自然数的约数中都有1和它本身,我们把小于它本身的因数叫做这个自然数的真约数. 如6的所有真 ...

  3. SDUT2141数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<cstdio> #include& ...

  4. Ubuntu环境下手动配置HBase0.94.25

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

  5. Android 注意在finish Activity之后也要停止正在运行的请求

    如果在一个Activity里面启动了网络请求,而在这个网络请求还没返回结果的时候,如果Activity被结束了,则我们需要写如下代码作为防守: @Override public void onPost ...

  6. hadoop集群基本配置

    最近在学习hadoop.网上具体过程很多,我就说说简单过程和注意问题. 环境:宿主机(windows64),虚拟机(centos64). 准备软件: 1.Vmware——虚拟机 2.centos镜像文 ...

  7. leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  8. [hackerrank]John and GCD list

    https://www.hackerrank.com/contests/w8/challenges/john-and-gcd-list 简单题,GCD和LCM. #include <vector ...

  9. *[topcoder]LongWordsDiv2

    http://community.topcoder.com/stat?c=problem_statement&pm=13147 此题关键在于发现ABAB的组合最多有26*26种,可以穷举,然后 ...

  10. 包装类型的比较,如:Integer,Long,Double

    Integer, Long, Double等基本类型的包装类型,比较时两种方法:第一种:equals,  第二种: .intValue(),  .longValue() ,  .doubleValue ...