题目:

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1

分析与解答:

1.二叉树有根结点,有两个子结点,而且子结点数据类型和根节点相同,因此我们用struct存储
2.根据题中给的数据建树,先序后序中序,知道任意两个就可以建树
这题是给了中序和先序,左根右,根左右
主要问题就是根据遍历的性质找根,然后递归建立左子树右子树

先序:1 2 4 7 3 5 8 9 6
中序:4 7 2 1 8 5 9 3 6
先序第一个1是根
所以从中序开始找,472应当是第一个根的左子树
对应先序的247,可知,如果把左子树从新看成二叉树,那他的根就是2
再继续找,把左子树找完了
右子树35896 根是3,然后继续找

总结一下思想:
先创一个结点,他的根直接知道就是先序第一个数,先认为他没有左右子结点(因为如果递归到最后一个子叶,他没有子结点)
找到根后,通过递归调用,填他的左子树的结点值右子树的结点值

输出的话如果后序,输出:左子树,右子树,根
这题是由于空格的原因,我把数存到数组里了

#include <iostream>
#include <cstdio>
#include <cstring>
#include<cstdlib>
#include <algorithm>
#define N 1000
using namespace std;struct TreeNote
{int ch;        TreeNote * ltree;TreeNote * rtree;
};
TreeNote * CreatTree(int * preorder, int * inorder, int longth)
{TreeNote * sontree = new TreeNote;sontree-> ch = *preorder;sontree-> ltree = NULL;sontree-> rtree = NULL;if(longth == 0)return NULL;int index = 0;for(; index < longth; index++)if(inorder[index] == *preorder)break;sontree-> ltree = CreatTree(preorder+1, inorder, index);sontree-> rtree = CreatTree(preorder+index+1, inorder+index+1, longth-index-1);return sontree;
}
int a[1000];
int p=0;
void PostOrder(TreeNote * tone)
{if(tone){PostOrder(tone-> ltree);PostOrder(tone-> rtree);a[p++]=tone->ch;}
}int main()
{int preorder[N];int inorder[N];int t; while(cin>>t){p=0;memset(a,0,sizeof(a));for(int i=0;i<t;++i){cin>>preorder[i];}for(int i=0;i<t;++i){cin>>inorder[i];}TreeNote * tone = CreatTree(preorder, inorder, t);PostOrder(tone);for(int i=0;i<p;++i){if(i!=p-1) cout<<a[i]<<' ';else cout<<a[i];} putchar('\n');}return 0;
}

(二叉树存储+递归遍历)Binary Tree Traversals相关推荐

  1. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  2. hdu 1710 Binary Tree Traversals (二叉树)

    1 /********************************************************** 2 题目: Binary Tree Traversals(hdu 1710) ...

  3. C++版二叉树非递归遍历

    C++版二叉树非递归遍历 文章目录 C++版二叉树非递归遍历 一.二叉树前序遍历 二.二叉树中序遍历 三.二叉树后序遍历 一.二叉树前序遍历 /*** Definition for a binary ...

  4. 代码随想录day13|二叉树理论基础、二叉树的递归遍历、二叉树的迭代遍历

    二叉树理论基础 这边需要重点注意的是二叉树的链式节点的定义 struct TreeNode(){int val;TreeNode *left;TreeNode *right;TreeNode(int ...

  5. C语言-数据结构-二叉树的递归遍历和非递归遍历

    看了大量网络相关的理论和程序,多数的C++ 写的,这里使用devC++ 编程语言 C语言; 整合王道考研答案和理论, 还有小甲鱼的数据结构, 郝斌的数据结构,各有特点吧 最值得研究的还是后序遍历的非递 ...

  6. 数据结构——二叉树的递归遍历算法与非递归遍历算法+层次遍历算法

    (文章篇幅有点长,二叉树的递归遍历算法不作详细分析,但是二叉树的非递归遍历算法和层次遍历算法都有非常详细的分析过程,记得往下翻哦!) 二叉树的递归遍历算法实现 我们首先用递归的方法先序遍历创建这样一棵 ...

  7. 二叉树的递归遍历和层序遍历(c/c++)

    递归遍历: 二叉树的三种递归遍历为先序遍历,中序遍历和后续遍历.它们相似之处在于都是对二叉树的递归遍历且对任何一个结点都经过三次,区别之处在于哪一次对该结点进行访问,由此分为先,中,后序遍历.所以对于 ...

  8. 一种二叉树非递归遍历的简单写法

    一种二叉树非递归遍历的简单写法 目录 一种二叉树非递归遍历的简单写法 先序遍历 中序遍历 后序遍历 二叉树的遍历是数据结构中非常基础的一个知识点,也是面试手撕代码环节的一个常见题目.这个问题的递归写法 ...

  9. 二叉树非递归遍历(模版)

    读完本篇内容大约花费您7分钟时间 本文主要讲解二叉树非递归遍历,由于是非递归遍历,所以需要用到栈stack,我们如果仔细考虑递归遍历的代码,就能明白非递归种栈的应用. 由于几种遍历方式只是在处理中间节 ...

最新文章

  1. 计算机二级c语言复习计划,全国计算机等级考试二级C语言考试复习资料与复习计划以与考试要点.doc...
  2. matlab二元方程组,用matlab解一个二元方程组,会的进,得到解再回答
  3. 如何在当前目录快速打开cmd
  4. 读《驯服烂代码——在编程操练中悟道》
  5. 36 万美元套利!3 步骤揭秘黑客 DeFi 闪电贷全过程
  6. 股票软件开发中全推与点播的区别
  7. 测试与 debug 心得
  8. 【w3cschool】C语言复习
  9. 【解读】Http协议
  10. 如何解决mac在访达里不可以新建txt文本的问题?
  11. WebService:JAX-WS实现WebService
  12. FreeType需要libpng的说明(编译时可以用参数去掉,2.12已支持svg)
  13. 全局唯一标识符 (GUID)
  14. 魔术师usm安装服务器系统,系统总裁开发的u盘魔术师安装win7图文教程
  15. 【VBScript恶搞代码】电脑自爆警告
  16. CUDA+Pycharm-gpu版本+Anaconda安装
  17. mysql插件的初始化
  18. 3. Zigbee应用程序框架开发指南 - 应用程序框架目录结构
  19. 5分钟快速学会使用Swiper.js,实现轮播图各种效果!
  20. ubuntu 选择独立显卡或则intelcpu内集成显卡

热门文章

  1. 《数据结构与算法分析-C语言描述》习题2.6
  2. xml文件转换成图片_怎样能把PDF文件转换成图片?
  3. php 循环大数组 卡死,PHP 大数组循环问题_PHP教程
  4. MYSQL数据库实验三多表查询_数据库之 MySQL --- 数据处理 之多表查询 (三)
  5. python编程快速上手 让繁琐工作自动化 豆瓣_2019年,这些豆瓣评分9.0以上的8本程序员好书你都知道吗?...
  6. android 动态库 后缀,Android Robolectric加载运行本地So动态库
  7. numpy 平方_Numpy的终极备忘录
  8. 大地最新win11 32位专业版镜像v2021.07
  9. 腾讯视频如何设置定时关机
  10. 【Java深入理解】String str = “a“ + “b“ + “c“到底创建了几个对象?