题目链接: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. JavaScript学习笔记--ES6学习(五) 数值的扩展

    ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...

  2. java dos下中文乱码

    代码如下: public class PrintString{ public static void main(String args[]){ System.out.println("\\* ...

  3. 3 - testng.xml

    TestNG的调用有以下几种方式: testng.xml ant 命令行 这部分主要介绍testng.xml的格式. 当前testng.xml的DTD(文档类型定义(Document Type Def ...

  4. jQuery判断文本框是否为空

    1.引用jQuery库 <script src="/static/js/jquery_v1.6.1.js" type="text/javascript"& ...

  5. 数组去重的三种方法及from方法

    直接上代码: var str="adbbckddwerivka"; var arr=str.split(""); console.log(arr); //ind ...

  6. Ueditor之SAE移植

    新浪SAE环境下使用UEditor http://www.cnblogs.com/zjzhome/p/3815460.html?utm_source=tuicool 在SAE上使用Ueditor的图片 ...

  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. Day12 线程池、RabbitMQ和SQLAlchemy

    1.with实现上下文管理 #!/usr/bin/env python# -*- coding: utf-8 -*-# Author: wanghuafeng #with实现上下文管理import c ...

  10. Unix/Linux 'dirctory tree' command.

    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...