preOrder 5 3 2 4 8 6 9   midOrder 2 3 4 5 6 8 9  postOrder 2 4 3 6 9 8 5

#include <iostream>
#include <cstdio>
using namespace std; const int maxn = ; typedef struct Node
{
int key;
struct Node *left;
struct Node *right;
}treeNode; int midOrder[maxn];
int postOrder[maxn]; // 由后序和中序,得到前序
treeNode *createTree(int midLeft, int midRight, int postLeft, int postRight)
{
if (postRight - postLeft < ) return NULL;
treeNode *root = new treeNode;
root->key = postOrder[postRight];
if (postLeft == postRight)
{
root->left = NULL;
root->right = NULL;
}
int index;
for (index = midLeft; index <= midRight; ++index)
{
if (midOrder[index] == postOrder[postRight]) break;
}
root->left = createTree(midLeft, index - , postLeft, postLeft + index - midLeft - );
root->right = createTree(index + , midRight, postLeft + index - midLeft, postRight - );
return root;
} void preOrder(treeNode *root)
{
if (root != NULL)
{
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
} int main()
{
    /*************************
      test.txt 文件内容如下:

        2 3 4 5 6 8 9
        2 4 3 6 9 8 5
**************************/
    freopen("test.txt", "r", stdin);
int n;
cin >> n; for (int i = ; i < n; ++i)
cin >> midOrder[i]; for (int i = ; i < n; ++i)
cin >> postOrder[i]; treeNode *root = createTree(, n - , , n - );
cout << "The preOrder: " << endl;
preOrder(root);
cout << endl;
return ; }

后序/中序--->前序的更多相关文章

  1. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

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

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

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  4. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  5. SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...

  6. 【IT笔试面试题整理】给定二叉树先序中序,建立二叉树的递归算法

    [试题描述]:  给定二叉树先序中序,建立二叉树的递归算法 其先序序列的第一个元素为根节点,接下来即为其左子树先序遍历序列,紧跟着是右子树先序遍历序列,固根节点已可从先序序列中分离.在中序序列中找到 ...

  7. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  8. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  9. UVa 二叉树重建(先序+中序求后序)

    题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...

随机推荐

  1. (09)odoo工作流

    --------------修订时间14:54 2016-09-18 星期日21:59 2016-06-12 星期日10:06 2016-02-24 星期三17:14 2016-01-29 星期五-- ...

  2. 杂记(编程style)----google code style!

    1.文件名 使用小写字母和下划线组合.头文件以.h结尾,定义文件用.cc结尾.例如:my_useful_class.cc 2.类型名 使用大写字母开头,多个单词组合时每个单词的首字母大写.例如:Url ...

  3. HDUOJ---1236 排名(浙大考研题)

    排名 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  4. 231. Power of Two 342. Power of Four -- 判断是否为2、4的整数次幂

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  5. JavaScript EventLoop

    转自:http://cek.io/blog/2015/12/03/event-loop/ What is JavaScript What is JavaScript anyway? Some word ...

  6. (转)onTouchEvent方法的使用

      (转)onTouchEvent方法的使用 手机屏幕事件的处理方法onTouchEvent.该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触 ...

  7. PHP慢脚本日志和Mysql的慢查询日志(转)

      1.PHP慢脚本日志 间歇性的502,是后端 PHP-FPM 不可用造成的,间歇性的502一般认为是由于 PHP-FPM 进程重启造成的. 在 PHP-FPM 的子进程数目超过的配置中的数量时候, ...

  8. js打印对象(object)

    function printObject(obj){//obj = {"cid":"C0","ctext":"区县"}; ...

  9. HDU 1560 DNA sequence A* 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 ...

  10. HDU 2181 哈密顿绕行世界问题 dfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2181 只有20个城市,而且每个点的度数恰好是3,也就意味着,对于即将进入环中的点,入度1,出度2,下一个点只有两 ...