题目链接: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. UITextView/UITextField检测并过滤Emoji表情符号

    UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...

  2. 1.swt/rap学习源码网址

    1.rap使用JS/ RAP加载JS http://download.eclipse.org/rt/rap/doc/2.3/guide/reference/jsdoc/symbols/rap.html ...

  3. 非注解SpringMVC

    <!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> < ...

  4. Linux下ln链接命令详解

    ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...

  5. Android SlidingMenu开源库及其使用

    极客学院教程: http://www.jikexueyuan.com/course/61_5.html?ss=1 1. SlidingMenu开源库的配置 2. SlidingMenu 的使用 --- ...

  6. nav

    $(document).ready(function() { $(window).resize(function(){ var need=0; var ul_max_width = $(window) ...

  7. ubuntu更新源

    源一定要找对应的版本 14.04对应 trusty deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multive ...

  8. MySQL账号授权操作

    Mysql权限控制 - 允许用户远程连接 设置mysql root密码: mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = P ...

  9. 简单工厂模式的C++实现

    用简单工厂模式实现一个计算器类: #include <iostream> #include <string> using namespace std; class Operat ...

  10. iOS:KVO/KVC 的概述与使用

    iOS:KVO/KVC 的概述与使用       KVO   APP开发技术QQ群:347072638 一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性 ...