POJ 1577Falling Leaves(二叉树的建立)
题目链接: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:
- It may be empty.
- 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:
- Empty trees are omitted completely.
- 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:
- If the tree is empty, then the preorder traversal is empty.
- 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 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
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(二叉树的建立)的更多相关文章
- C语言二叉树的建立与遍历
二叉树的建立和遍历都要用到递归,先暂时保存一下代码,其中主要是理解递归的思想,其它的就都好理解了.这里是三种遍历方式,其实理解一种,其它的几个就都理解了,就是打印出来的顺序不一样而已.建立和遍历的方式 ...
- C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)
树 利用顺序存储和链式存储的特点,可以实现树的存储结构的表示,具体表示法有很多种. 1)双亲表示法:在每个结点中,附设一个指示器指示其双亲结点在数组中的位置. 2)孩子表示法:把每个结点的孩子排列起来 ...
- 二叉树的建立以及遍历的多种实现(python版)
二叉树是很重要的数据结构,在面试还是日常开发中都是很重要的角色. 首先是建立树的过程,对比C或是C++的实现来讲,其涉及到了较为复杂的指针操作,但是在面向对象的语言中,就不需要考虑指针, 内存等.首先 ...
- 一步一步写数据结构(二叉树的建立和遍历,c++)
简述: 二叉树是十分重要的数据结构,主要用来存放数据,并且方便查找等操作,在很多地方有广泛的应用. 二叉树有很多种类,比如线索二叉树,二叉排序树,平衡二叉树等,本文写的是最基础最简单的二叉树. 思路: ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- 二叉树的建立&&前中后遍历(递归实现)&&层次遍历
下面代码包含了二叉树的建立过程,以及三种遍历方法了递归实现,代码中还利用队列实现了层次遍历. import java.util.LinkedList; import java.util.Queue; ...
- 二叉树的建立与遍历(c语言)入门
树其实在本质上就是一对多,链表就是一对一. 二叉树的建立: 这里的代码采用的是最粗暴的创建方法,无实际用处.但初次学习二叉树可以通过这个创建方法更好的理解二叉树. 二叉树的遍历: 遍历在大体上分为递归 ...
- c语言_二叉树的建立以及3种递归
二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{ int data; struct node* left; struct node* ri ...
- 二叉树的建立与遍历(山东理工OJ)
题目描写叙述 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(当中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度. 输入 输入一个长度 ...
随机推荐
- laravel配置路由出现404
nginx配置上加一句话 location / { #try_files $uri $uri/ =; try_files $uri $uri/ /index.php?$query_string; }
- Mint UI 之 Swipe 组件
#为什么不显示内容? 一定要指定 mt-swipe 元素的宽和高. <mt-swipe :auto="4000" class="swipe"> &l ...
- vue.js项目打包上线
最近一直坚持每个月写一个小的vue.js 开发的项目,最后开发完成后想到很久之前给别人回答的一个问题:vue的项目如何上线,当时有千奇百怪的回答,我在想,这些人都是肿了么,vue的官方都说了,这个框架 ...
- Java NIO系列教程(四) Scatter 和 Gather
Java NIO系列教程(四) Scatter 和 Gather Java NIO 开始支持 scatter/gather,scatter/gather 用于描述从 Channel(译者注:Chann ...
- Can't use Subversion command line client: svn. Errors found while svn working copies detection.
idea 报错: Can't use Subversion command line client: svn. Errors found while svn working copies detect ...
- centos 挂载u盘
1.创建一个目录来挂载U盘 mkdir /mnt/usb #创建usb目录挂载U盘 2.插上U盘,查看移动设备状态 fdisk -l #(注意:参数是小写字母 l 不是数字 1) 会看到类似这一行:/ ...
- 深入理解line-height与vertical-align(1)
http://www.cnblogs.com/xiaohuochai/p/5271217.html
- 2018.10.19 NOIP模拟 加密(模拟)
传送门 直接按hashhashhash函数反着算回去就行了. 加法用exgcdexgcdexgcd,异或直接枚举二进制位. 代码
- 2018.08.27 rollcall(非旋treap)
描述 初始有一个空集,依次插入N个数Ai.有M次询问Bj,表示询问第Bj个数加入集合后的排名为j的数是多少 输入 第一行是两个整数N,M 接下来一行有N个整数,Ai 接下来一行有M个整数Bj,保证数据 ...
- Spring MVC之@RequestMapping 传递数组
1.前台: var param = {titles:['col1','col2','col3']}; $.ajax({url:url, type:"post", data:para ...