描述

给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度≤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. [LeetCode]Link List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  2. APT攻击

    http://netsecurity.51cto.com/art/201211/363040.htm

  3. poj 3150 Cellular Automaton

    首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...

  4. [2-sat]HDOJ1824 Let's go home

    中问题 题意略 和HDOJ 3062一样 这里 每个队员都有 选 和 不选 两种, 即 上篇所说的$x$和$x’$ 建图:队长(a)留下或者其余两名队员(b.c)同时留下 那么就是$a' \Right ...

  5. hdu1151 Air Raid

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 增广路的变种2:DAG图的最小路径覆盖=定点数-最大匹配数 #include<iostream> ...

  6. 机器学习第三课(EM算法和高斯混合模型)

    极大似然估计,只是一种概率论在统计学的应用,它是参数估计的方法之一.说的是已知某个随机样本满足某种概率分布,但是其中具体的参数不清楚,参数估计就是通过若干次试验,观察其结果,利用结果推出参数的大概值. ...

  7. PowerDesigner模型设计

    原文:PowerDesigner模型设计 绪论 Sybase PowerDesigner(简称PD)是最强大的数据库建模工具,市场占有率第一,功能也确实十分强大,现在最新版本是15.1,已经支持最新的 ...

  8. iOS iOS7越狱

    1.使用盘古越狱工具 (或者PP助手) 2.越狱成功后需要安装Apple File Conduit “2”,用于替代afc2add插件 3.安装AppSync插件 (绕过系统验证,随意安装.运行破解的 ...

  9. Failed to allocate memory: 8

    Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual ...

  10. android4.4内核移植

    01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...