后序/中序--->前序
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 二叉树重建(先序+中序求后序)
题意是给出先序和中序,求出后序. 先序遍历先访问根结点,通过根结点可以在中序中把序列分为左子树部分和右子树部分,我建了一个栈,因为后序遍历最后访问根结点,所以把每次访问的根结点放入栈中.因为后序遍历先 ...
随机推荐
- Java线程与多线程教程
本文由 ImportNew - liken 翻译自 Journaldev. Java线程是执行某些任务的轻量级进程.Java通过Thread类提供多线程支持,应用可以创建并发执行的多个线程. 应用 ...
- 1106c语言语法树
- CentOS所有下载
简述 CentOS(Community Enterprise Operating System - 社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux ...
- hdu------(1525)Euclid's Game(博弈决策树)
Euclid's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- target不起作用了
原因是 <a href="",target></a>中间多了个逗号.
- ajax的data传参的两种方式
ajax的data传参的两种方式 本文为转载. 1.[javascript] view plain copy /** * 订单取消 * @return {Boolean} 处理是否成功 */ func ...
- 10个必备的移动UI设计资源站(转)
创建移动设计模式是非常重要的一步,记住!这是为移动设备设计而不是web.不仅仅是移动屏幕远小于普通的电脑屏幕,关键是鼠标和键盘已经被手指替代了! 当然还有更重要的,说起来很苦逼,我们再也不能使用一种模 ...
- C# + winserver2008 openfiledialog 写入 textbox1 中的 路径不正确
System.IO.Path.GetFullPath(openFileDialog1.FileName);//绝对路径 System.IO.Path.GetExtension(openFileDial ...
- webapi方式
随笔 - 112 文章 - 0 评论 - 334 ASP.NET MVC学习系列(二)-WebAPI请求 继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前 ...
- svn resolve/merge
svn merge http://svn.a.com/branches/20150129_168954_sales-impr_1 svn resolve --accept working web/sr ...