Vijos1132:求二叉树的先序序列
描述
给出一棵二叉树的中序与后序排列。求出它的先序排列。(约定树结点用不同的大写字母表示,长度≤8)。
格式
输入格式
第一行为二叉树的中序序列
第二行为二叉树的后序序列
输出格式
一行,为二叉树的先序序列
BADC
BDCA
output:
ABCD
思路:先确定树根,再确定左子树与右子树的范围。
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
char in_order[MAXN];
char post_order[MAXN];
char pre_order[MAXN];
void build(int l1,int r1,int l2,int r2)
{
if(l1>r1)
return ;
char root=post_order[r2];
int p=l1;
while(in_order[p]!=root) p++;
printf("%c",root);
int cnt=p-l1;
build(l1,p-,l2,l2+cnt-);//左子树
build(p+,r1,l2+cnt,r2-);//右子树
}
int main()
{
scanf("%s",in_order);
scanf("%s",post_order);
int len=strlen(in_order);
build(,len-,,len-);
return ;
}
下面是利用中序遍历与前序遍历求后序遍历
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
char in_order[MAXN];
char pre_order[MAXN];
void build(int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2)
return ;
char root=pre_order[l2];
int p=l1;
while(in_order[p]!=root) p++;
int cnt=p-l1;
build(l1,p-,l2+,l2+cnt);
build(p+,r1,l2+cnt+,r2);
printf("%c",root);
}
int main()
{
scanf("%s",in_order);
scanf("%s",pre_order);
int len=strlen(in_order);
build(,len-,,len-);
return ;
}
Vijos1132:求二叉树的先序序列的更多相关文章
- Vijos 1132 求二叉树的先序序列
描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度≤8). 格式 输入格式 第一行为二叉树的中序序列 第二行为二叉树的后序序列 输出格式 一行,为二叉树的先 ...
- 【递归】Vijos P1132 求二叉树的先序序列(NOIP2001普及组第三题)
题目链接: https://vijos.org/p/1132 题目大意: 给定二叉树的中序和后序遍历,求该二叉树先序遍历. 题目思路: [递归] 这题妥妥递归. 二叉树先序根左右,中序左根右,后序左右 ...
- hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)
http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- SDUTOJ 1489 求二叉树的先序遍历
<img src="http://img.blog.csdn.net/20141014212549703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...
- 数据结构实验之求二叉树后序遍历和层次遍历(SDUT 2137)
Problem Description 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历. Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据. ...
- 分别求二叉树前、中、后序的第k个节点
一.求二叉树的前序遍历中的第k个节点 //求先序遍历中的第k个节点的值 ; elemType preNode(BTNode *root,int k){ if(root==NULL) return ' ...
- 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树
已知 中序&后序 建立二叉树: SDUT 1489 Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t& ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- php中的字符串和正則表達式
一.字符串类型的特点 1.PHP是弱类型语言,其它数据类型一般都能够直接应用于字符串函数操作. 1: <? php 2: echo substr("123456",2,4); ...
- XP,32/64位Win7,32/64位Win10系统【电脑城版】
本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...
- Android应用开发:网络工具——Volley(二)
引言 在Android应用开发:网络工具--Volley(一)中结合Cloudant服务介绍了Volley的一般使用方法.当中包括了两种请求类型StringRequest和JsonObjectRequ ...
- 导出到Excel中NPOI
源地址:http://www.cnblogs.com/dreamof/archive/2010/06/02/1750151.html\ 1.NPOI官方网站:http://npoi.codeplex. ...
- python 基础 9.3 mysql 数据操作
#/usr/bin/python #coding=utf-8 #@Time :2017/11/21 0:20 #@Auther :liuzhenchuan #@File :mysql 数据操作 ...
- 【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT
[BZOJ3435][Wc2014]紫荆花之恋 Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从 ...
- 【BZOJ3721】PA2014 Final Bazarek 贪心
[BZOJ3721]PA2014 Final Bazarek Description 有n件商品,选出其中的k个,要求它们的总价为奇数,求最大可能的总价. Input 第一行一个整数n(1<=n ...
- quick-cocos2d-x教程11:实现http通信,并与站点php对接,可实现登录等常见功能
手机游戏眼下是弱联网居多,http登录是经常使用功能.我们如今就来实现. 在启动时候.自己主动请求http. function MainScene:ctor() local url = &qu ...
- Android之Handler使用方法总结
方法一:(java习惯,在android平台开发时这样是不行的,由于它违背了单线程模型) 刚刚開始接触android线程编程的时候,习惯好像java一样,试图用以下的代码解决这个问题 new T ...
- Java for LeetCode 110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...