后序/中序--->前序
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 ; }
后序/中序--->前序的更多相关文章
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...
- 【IT笔试面试题整理】给定二叉树先序中序,建立二叉树的递归算法
[试题描述]: 给定二叉树先序中序,建立二叉树的递归算法 其先序序列的第一个元素为根节点,接下来即为其左子树先序遍历序列,紧跟着是右子树先序遍历序列,固根节点已可从先序序列中分离.在中序序列中找到 ...
- SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- UVa 二叉树重建(先序+中序求后序)
题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...
随机推荐
- 笔记13:File 类的一些操作
一.对文件的创建(create) private void button1_Click(object sender, EventArgs e) { File.Create(@"F:\\QQP ...
- 获取txt文件指定行内容
#!/usr/bin/python num=0; ni=open("C:\Python34\ceshi.txt") for line in ni: num=num+1; #表示行 ...
- Oracle函数大全之转换函数
chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SELECT ...
- hive分区导致FAILED: Hive Internal Error: java.lang.NullPointerException(null)
写了一条hive sql ,其中条件中存在 dt>=20150101 and dt<=20150228 这样的条件,原来执行没问题,今天就抛出 FAILED: Hive Internal ...
- 表单重置reset
最近一直在做各种页面的“增删改查”,只是在做新增功能的时候,自己一直在使用 reset来清空form表单,原以为这样子的清空方法是万无一失的,可惜最终还是在进行“修改”操作完了之后再“新增”的时候,就 ...
- http://ecgui.com/?hg=0&nr=0
eCGUI- 微型嵌入式GUI体积小巧 大小在 100KB~180KB! 特性: 多窗口支持| 完全中文输入/显示| 多数常用GUI控件| 99.9% ANSI C 编写| 已成功移植 DOS,Lin ...
- JDE910笔记2--OMW项目建立及简单使用[转]
1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...
- java入门第四步之应用服务器的安装(Tomcat)【转】
首先打开myeclipse,在myeclipse菜单栏下面有两个按钮: 左边的按钮是进行项目的部署的(即将项目部署到服务器上),右边的按钮是进行服务器的启动的,如果你安装了tomcat服务器,那你就可 ...
- Eclipse导出可执行Jar文件(包含第三方Jar包)
1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...
- [示例]NSEnumerator-使用枚举类型实现数组的逆序输出
代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...