题目链接:http://poj.org/problem?id=1577

解题思路:题目是中文就不翻译了,网上都说这题很水,但本蒟蒻虽然知道是倒过来建立二叉搜索树,可是实现不了,得到小伙伴的关键递归思想的指点之后,茅塞顿开,豁然开朗,终于也能笑谈,曰之"水题"!!!

Falling Leaves

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5340   Accepted: 2886

Description

 
Figure 1
Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and go right to The problem.

A binary tree of letters may be one of two things: 

  1. It may be empty.
  2. It may have a root node. A node has a letter as data and refers to a left and a right subtree. The left and right subtrees are also binary trees of letters.

In the graphical representation of a binary tree of letters: 

  1. Empty trees are omitted completely.
  2. Each node is indicated by 
    • Its letter data,
    • A line segment down to the left to the left subtree, if the left subtree is nonempty,
    • A line segment down to the right to the right subtree, if the right subtree is nonempty.

A leaf in a binary tree is a node whose subtrees are both empty. In the example in Figure 1, this would be the five nodes with data B, D, H, P, and Y.

The preorder traversal of a tree of letters satisfies the defining properties: 

  1. If the tree is empty, then the preorder traversal is empty.
  2. If the tree is not empty, then the preorder traversal consists of the following, in order 
    • The data from the root node,
    • The preorder traversal of the root's left subtree,
    • The preorder traversal of the root's right subtree.

The preorder traversal of the tree in Figure 1 is KGCBDHQMPY.

A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies:

The root's data comes later in the alphabet than all the data in the nodes in the left subtree.

The root's data comes earlier in the alphabet than all the data in the nodes in the right subtree.

The problem:

Consider the following sequence of operations on a binary search tree of letters

Remove the leaves and list the data removed 
Repeat this procedure until the tree is empty 
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree 

by removing the leaves with data

BDHPY 
CM 
GQ 
K

Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.

Input

The input will contain one or more data sets. Each data set is a sequence of one or more lines of capital letters.

The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk ('*').

The last data set is followed by a line containing only a dollar sign ('$'). There are no blanks or empty lines in the input.

Output

For each input data set, there is a unique binary search tree that would produce the sequence of leaves. The output is a line containing only the preorder traversal of that tree, with no blanks.

Sample Input

BDHPY
CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

代码:

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int n;
char a[];
struct node
{
int l,r;
char f;
}tree[];
void Build_Tree(int index,char str)
{
if(tree[index].f==) //虽然f是char类型,但是还是有对应的ASCII值,所以能初始化0
{
tree[index].f=str;
return ;
}
else if(tree[index].f<str) //由题意可得大的在右边
{
if(!tree[index].r) tree[index].r=n++;
Build_Tree(tree[index].r,str);
}
else
{
if(!tree[index].l) tree[index].l=n++;
Build_Tree(tree[index].l,str);
}
}
void Printf(int root)
{
if(!tree[root].f) return ;
//因为题目求的是先序输出,所以这样排,交换后三句位置就是不同顺序遍历
//先序输出,根左右,恩,就是这样
printf("%c",tree[root].f);
if(tree[root].l) Printf(tree[root].l);
if(tree[root].r) Printf(tree[root].r);
}
int main()
{
while(~scanf("%s",a))
{
char x; n=;
int len=strlen(a);
memset(tree,,sizeof(tree));
while()
{
scanf("%c",&x);
if(x>='A'&&x<='Z') a[len++]=x;
if(x=='*'||x=='$') break;
}
//反过来建树
for(int i=len-; i>=; i--) Build_Tree(,a[i]);
Printf();
cout<<endl;
if(x=='$') break;
}
return ;
}

POJ 1577Falling Leaves(二叉树的建立)的更多相关文章

  1. C语言二叉树的建立与遍历

    二叉树的建立和遍历都要用到递归,先暂时保存一下代码,其中主要是理解递归的思想,其它的就都好理解了.这里是三种遍历方式,其实理解一种,其它的几个就都理解了,就是打印出来的顺序不一样而已.建立和遍历的方式 ...

  2. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)

    树 利用顺序存储和链式存储的特点,可以实现树的存储结构的表示,具体表示法有很多种. 1)双亲表示法:在每个结点中,附设一个指示器指示其双亲结点在数组中的位置. 2)孩子表示法:把每个结点的孩子排列起来 ...

  3. 二叉树的建立以及遍历的多种实现(python版)

    二叉树是很重要的数据结构,在面试还是日常开发中都是很重要的角色. 首先是建立树的过程,对比C或是C++的实现来讲,其涉及到了较为复杂的指针操作,但是在面向对象的语言中,就不需要考虑指针, 内存等.首先 ...

  4. 一步一步写数据结构(二叉树的建立和遍历,c++)

    简述: 二叉树是十分重要的数据结构,主要用来存放数据,并且方便查找等操作,在很多地方有广泛的应用. 二叉树有很多种类,比如线索二叉树,二叉排序树,平衡二叉树等,本文写的是最基础最简单的二叉树. 思路: ...

  5. UVA.699 The Falling Leaves (二叉树 思维题)

    UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...

  6. 二叉树的建立&&前中后遍历(递归实现)&&层次遍历

    下面代码包含了二叉树的建立过程,以及三种遍历方法了递归实现,代码中还利用队列实现了层次遍历. import java.util.LinkedList; import java.util.Queue; ...

  7. 二叉树的建立与遍历(c语言)入门

    树其实在本质上就是一对多,链表就是一对一. 二叉树的建立: 这里的代码采用的是最粗暴的创建方法,无实际用处.但初次学习二叉树可以通过这个创建方法更好的理解二叉树. 二叉树的遍历: 遍历在大体上分为递归 ...

  8. c语言_二叉树的建立以及3种递归

    二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{    int data;    struct node* left;    struct node* ri ...

  9. 二叉树的建立与遍历(山东理工OJ)

    题目描写叙述 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(当中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度. 输入 输入一个长度 ...

随机推荐

  1. 求N的阶乘N!中末尾0的个数

    求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...

  2. CF 1023D Array Restoration - 线段树

    题解 非常容易想到的线段树, 还可以用并查集来. 还有一位大神用了$O(n)$ 就过了Orz 要判断是否能染色出输入给出的序列,必须满足两个条件: 1. 序列中必须存在一个$q$ 2. 两个相同的数$ ...

  3. IPEndPoint

    .NET框架用IPEndPoint 对象来表示一个特定的IP地址和端口的组合,应用该对象的场景多是在讲socket绑定到本地地址或者将socket绑定到非本地地址.

  4. circos 绘制关系型图ribbon,并加入透明度

    luminance = lum80<<include colors_fonts_patterns.conf>><colors># r,g,b,a color def ...

  5. 2018.08.21 bzoj4668: 冷战(并查集+启发式合并)

    传送门 可以发现需要维护连通性和两点连通时间. 前者显然是并查集的常规操作,关键就在于如何维护两点的连通时间. 然后会想到这个时候不能用路径压缩了,因为它会破坏原本树形集合的结构,因此可以启发式按si ...

  6. opengl中场景变换|2D与3D互转换(转)

    opengl中场景变换|2D与3D互转换 我们生活在一个三维的世界——如果要观察一个物体,我们可以: 1.从不同的位置去观察它.(视图变换) 2.移动或者旋转它,当然了,如果它只是计算机里面的物体,我 ...

  7. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  8. 虚拟化之Xen简介

    1>相关知识简介: 1>常用的磁盘IO调度器: CFQ:完全公平队列算法: deadline:最后期限算法: anticipatory:顺序读写队列算法/预期算法: NOOP:no  op ...

  9. 21 Guns -- Green Day

                            21 Guns Green Day (绿日乐队)的代表曲之一.歌曲的主题是反战,同时安慰了曾 经信任布什政府如今失望透顶的美国民众.这首歌也被电影< ...

  10. [jquery-ajax] jquery ajax 三种情况对比

    <button class="btn1">async:false</button> <button class="btn2"> ...