题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1136

题目描述:给定一个按照(左子树-右子树-根)(即先序)遍历序列的树,求其按照 右子树-左子树-根 遍历的结果。(每个数都不同)

题目思路:按照题目意思其实构造的是一个二叉查找树,满足左子树元素都不大于当前根的元素,右子树元素都不小于当前根的元素。

而且二叉查找树按照 中序遍历 的结果是元素按照递增顺序输出(二叉查找树的性质)。所以实际上又告诉了你中序遍历的结果(即把所给元素递增排序的结果)。

所以只要按照它给的后序遍历和隐含的中序遍历的序列,递归构造并输出就可以了。

如果不熟悉二叉树,这是几篇不错的教程:

http://blog.csdn.net/hinyunsin/article/details/6315502(评论指出了原文中的错误)

http://blog.csdn.net/pegasuswang_/article/details/10169397

http://www.slyar.com/blog/c-preord-inord-tree.html这个版本的代码比较容易理解

本题代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
const int M = 3010;
int postord[M], inord[M]; //后序和中序遍历序列
void build(int n, int *postord, int *inord)
{
if (n <= 0)
return;
int p = std::find(inord, inord + n, postord[n-1]) - inord; //计算根节点在中序遍历中的位置
build(n - p - 1, postord + p, inord + p + 1); //递归构造右子树的(右-左-根)遍历
build(p, postord, inord); //递归构造左子树的(右-左-根)遍历
printf("%d ", postord[n-1]); //输出根节点
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d", postord + i);
inord[i] = postord[i];
}
std::sort(inord, inord + n);
build(n, postord, inord);
return 0;
}

几点说明:如果不理解代码,就找简单数据在纸上试试。要不看代码是不容易理解的。

这一题我直接在构造的时候就输出了,比较简练。

int p = std::find(inord, inord + n, postord[n-1]) - inord;              //计算根节点在中序遍历中的位置
build(n - p - 1, postord + p, inord + p + 1);  //递归构造右子树的遍历
build(p, postord, inord);                                          //递归构造左子树的遍历
printf("%d ", postord[n-1]);          //输出根节点

这几句是关键代码:

build(n - p - 1, postord + p, inord + p + 1);   //递归构造右子树的遍历

参数n-p-1是 右 子树的结点个数,用来控制递归深度:post+p是新的 右子树 在 后序遍历 中的位置;inord+p+1是新的 右子树 在 中序遍历 中的位置。

build(p, postord, inord);                                          //递归构造左子树的遍历

参数n-p-1是 左 子树的结点个数,用来控制递归深度:post+p是新的 左子树 在 后序遍历 中的位置;inord+p+1是新的 左子树 在 中序遍历 中的位置。

最后输出根节点就行了。

调整三句的位置就可以实现任意六种遍历的次序。

当然也可以先构造,再输出,不过麻烦点。

代码来自/*gaoshangbo*/

//给定后序遍历和中序遍历,求先遍历右子树, 左子树, 根的序列
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int s[3005], t[3005];
typedef struct node
{
int data;
node *l;
node *r;
}node;
node *Creat(int *s,int *t,int n)
{
int *p;
node *q;
int k;
if(n<=0) return NULL;
q=new node;
q->data=*s;
for(p=t;p<t+n;p++)
if(*p==*s) break;
k=p-t;
q->l=Creat(s-n+k,t,k);
q->r=Creat(s-1,t+k+1,n-k-1);
return q;
}
void print(node *root)
{
if(root==NULL) return ;
print(root->r);
print(root->l);
printf("%d ",root->data);
}
int main()
{
node *root;
int n, i;
scanf("%d", &n);
for(i = 0; i < n; i ++)
{
scanf("%d", &s[i]);
t[i] = s[i];
}
sort(t, t+n);
root = Creat(s+n-1,t,n);
print(root);
printf("\n");
return 0;
}

ural 1136. Parliament的更多相关文章

  1. URAL 1136 Parliament 二叉树水题 BST后序遍历建树

    二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...

  2. URAL 1136 Parliament (DFS)

    题意 输入一棵树的后缀表达式(按左-右-中顺序访问),这棵树的每一个结点的数值都比它的左子树结点的数值大,而比它的右子树结点的数值小,要求输出其按右-左-中顺序访问的表达式.所有的数都为正整数,而且不 ...

  3. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  4. timus 1136 Parliament(e)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  5. 1136. Parliament(二叉树)

    1136 先由后左 再父 建一个二叉树 #include <iostream> #include<cstdio> #include<cstring> #includ ...

  6. ural 1073. Square Country

    1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...

  7. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  8. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  9. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

随机推荐

  1. c# 双问号运算

    model.id??0 ??运算:如果运算符左边的值为NULL侧返回右边的值,否则返回左边的值

  2. Oracle 左连接、右连接、全外连接、(+)号作用、inner join(等值连接) (转载)

    Oracle  外连接 (1)左外连接 (左边的表不加限制)       (2)右外连接(右边的表不加限制)       (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...

  3. Oracle实用技巧

    一. ORACLE SQL PLUS 使用技巧: ----①查找重复记录: SELECT DRAWING, DSNOFROM EM5_PIPE_PREFABWHERE ROWID!= (SELECT ...

  4. CSS布局模型思考

    flow模型:默认布局模型,元素从左向右.从上到下依次排列,块状元素独占一行.Position属性对应值static. float模型:主要效果是让本来独占一行的块状元素变成内联-块状元素,并到一排显 ...

  5. Linux下su与su -命令的区别

    在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用service命令了. 原因: su命令和su -命令 ...

  6. Proxy 模式

    在以下集中情况下可以用 Proxy模式解决问题: 1)创建开销大的对象时候,比如显示一幅大的图片,我们将这个创建的过程交给代理去完成,GoF 称之为虚代理(Virtual Proxy): 2)为网络上 ...

  7. Linux嘚瑟一时的Shared Object

    场景概述 近来接触node程序以及负责实现node扩展来对象本地SDK的调用,旨在借node及其第三方库来快速实现RESTful API以及给浏览器端使用.当然这中间研究工作耗了不少时间. 在实现目标 ...

  8. 仿照淘宝首页做的一个高度伪对齐demo

    功能就是当右边高度没有左边高的情况下做的一些处理,由于本人技术有限,不兼容所有浏览器, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...

  9. clipboard.js IE下 不允许复制时, 问题修改

    问题描述:https://github.com/zenorocha/clipboard.js/wiki/Known-IssuesOn IE9-11 there's a prompt that asks ...

  10. 安卓AVD使用建议

    问题描述:之前在安装了Android开发环境后,一开始并没有直接在Android手机和平板上进行调试,是使用的AVD模拟器工具.由于电脑的配置不是特别好,总感觉AVD的使用速度太慢,包括启动的时候还有 ...