描述

在数据结构中,遍历是二叉树最重要的操作之一。所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。

这里给出三种遍历算法。

1.中序遍历的递归算法定义:
     若二叉树非空,则依次执行如下操作:
         (1)遍历左子树;
         (2)访问根结点;
         (3)遍历右子树。
2.前序遍历的递归算法定义:
    若二叉树非空,则依次执行如下操作:
         (1) 访问根结点;
         (2) 遍历左子树;
         (3) 遍历右子树。
3.后序遍历得递归算法定义:
    若二叉树非空,则依次执行如下操作:
         (1)遍历左子树;
         (2)遍历右子树;
         (3)访问根结点。
现在给出一个二叉树的中序遍历和前序遍历。求它的后序遍历。

样例中的二叉树如下:

  1. a x
    / \ / \
    b c n u
    / \ / / \
    d e f l i
    /
    g

输入

输入有多组数据,第一行有一个整数n,表示有n组数据。每组数据两行,每行均是由a-z的字符组成的字符串,每个字母表示一个结点。其顺序,分别为树的中序遍历和前序遍历。长度小于27.

输出

对于每组数据,输出一行,树的后序遍历。

样例输入

2
dbgeafc
abdegcf
lnixu
xnliu

样例输出

dgebfca
linux
题意

已知中序前序求后序

题解

输出后序可以直接在递归里实现,算是有了个新操作

代码

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. string In,Per;
  6. void build(int L1,int R1,int L2,int R2)
  7. {
  8. if(L1>R1)return;
  9. int root=Per[L2];
  10. int p=L1;
  11. while(In[p]!=Per[L2])p++;
  12. int cnt=p-L1;
  13. //cout<<root;//前序
  14. build(L1,p-,L2+,L2+cnt);
  15. //cout<<root;//中序
  16. build(p+,R1,L2+cnt+,R2);
  17. cout<<root;//后序
  18. }
  19. int main()
  20. {
  21. int t;
  22. cin>>t;
  23. while(t--)
  24. {
  25. cin>>In>>Per;
  26. int n=In.size()-;
  27. build(,n,,n);
  28. cout<<endl;
  29. }
  30. return ;
  31. }

TZOJ 3209 后序遍历(已知中序前序求后序)的更多相关文章

  1. PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树

    Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...

  2. python应用-已知三角形的边长求他的面积和周长

    """ 已知三角形的边长求他的面积和周长 Author:罗万财 Date:2017-3-3 """ import math a=float( ...

  3. 2019-8-31-C#-已知点和向量,求距离的点

    title author date CreateTime categories C# 已知点和向量,求距离的点 lindexi 2019-08-31 16:55:58 +0800 2018-05-08 ...

  4. C语言:已知三角形三边长求面积

    //已知三角形三边长求面积 #include <stdio.h> #include <math.h> int main() { float a,b,c,p,s; int x=0 ...

  5. PAT1020 (已知中序,后序遍历转前序遍历)

    已知后序与中序输出前序(先序):后序:3, 4, 2, 6, 5, 1(左右根)中序:3, 2, 4, 1, 6, 5(左根右) 已知一棵二叉树,输出前,中,后时我们采用递归的方式.同样也应该利用递归 ...

  6. Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。

    You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...

  7. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  8. HLG2040二叉树遍历已知前中,求后

    二叉树的遍历 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 60(34 users) Total Accepted: 34(30 us ...

  9. 二叉树遍历(flist)(二叉树,已知中序层序,求先序)

    问题 C: 二叉树遍历(flist) 时间限制: 1 Sec  内存限制: 128 MB提交: 76  解决: 53[提交][状态][讨论版][命题人:quanxing][Edit] [TestDat ...

随机推荐

  1. 机器学习进阶-图像基本操作-边界补全操作 1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 进行边界的补零操作 2.cv2.BORDER_REPLICATE(边界补零复制操作)...

    1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 参数说明: i ...

  2. css样式表1

    1内联样式表 和html联合使用,控制精确,但是可重用性差,冗余多. <p style="font-size:14px;"></p> <div sty ...

  3. HTML:Registry design.(Include a simple web design use HTML)

    Registry design: I feel a little bored when I design this registry,so T design a simple website all ...

  4. ping ip多进程处理小程序

    最近,环境维护需要经常需要判断某些服务器上的IP是否可达,由于服务器数量较多,逐一手工ping检查太过繁琐.写个小程序使用. 实现和说明 1.使用配置文件ip.txt实现可配置指定ip列表. 2.利用 ...

  5. Linux IP和网关配置

    操作环境 SuSE11/SuSE10 配置方法一<永久有效,重启不失效> 通过修改/etc/sysconfig/network/ifcfg-eth*文件直接配置,服务器重启不失效,建议使用 ...

  6. ios instancetype 和 id 的异同

    1.0 相同点:都可以作为方法的返回类型 2.0 不同点: a.instancetype 可以返回和方法所在类相同类型的对象   id 只能返回未知类型的对象 b. instancetype 只能作为 ...

  7. poj2480-Longge's problem-(欧拉函数)

    Longge is good at mathematics and he likes to think about hard mathematical problems which will be s ...

  8. servlet 3.1 摘录

    https://www.oschina.net/translate/non-blocking-io-using-servlet-3-1?cmp 非阻塞IO AsyncContext context = ...

  9. php分割最后一个逗号后面的字符

    $str = 'a/b/c/d/e/f'; echo preg_replace('/.*\//','',$str);     echo preg_replace('/.*,/','',$str);最后 ...

  10. Ubuntu系统下手动释放内存

    有时候,像mongo这种,对内存只吃不吐的,我们要手动释放一下. drop_caches的详细文档如下:Writing to this will cause the kernel to drop cl ...