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,,,(当中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度. 输入 输入一个长度 ...
随机推荐
- Cairo编程
一.简介 cairo 是一个免费的矢量绘图软件库,它可以绘制多种输出格式.cairo 支持许多平台,包括 Linux.BSD.Microsoft® Windows® 和 OSX(BeOS 和 OS2 ...
- 优秀UX设计师的八条黄金法则
与用户保持亲密 成为成功的UX设计师最重要的先决条件之一就是与用户保持紧密的联系,以发现和了解他们的需求和爱好.理想情况下你应该让自己完全地成为产品用户,因为只有这样你才能理解背后的动机.“这样的 ...
- 给Array添加去重原型方法
Array.prototype.unique = function(){ var newArray = []; var oldArray = this; if(oldArray.length<= ...
- install pip(mac)
simple method: sudo easy_install pip you have done!and can install the other py programs using pip ...
- devexpress v14.2.3 发布
补丁而已. New Major Features in 14.2 What's New in VCL Products 14.2 Breaking Changes To learn about bre ...
- 《Delphi XE6 android 编程入门教程》推荐
近5.6年已经没有看见关于delphi的新技术的书出来了(看来在国内delphi的使用量确实很低了), 高勇同学最近出了一本<Delphi XE6 android 编程入门教程>,上周刚拿 ...
- POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)
题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...
- *C语言的小技巧
计算数组长度 ,,,,}; int Length=sizeof(a)/sizeof(int); 交换a和b的值,不借用辅助变量 a=a+b; b=a-b; a=a-b; 将0-9的字符转化为整数 '; ...
- Bluebird-NodeJs的Promise
Promise是异步代码实现控制流的一种方式.这一方式可以让你的代码干净.可读并且健壮. 比如,你用来异步处理文件事件的回调代码: fs.readFile('directory/file-to-rea ...
- (转)code first基础
转自:http://tech.it168.com/a2011/0719/1220/000001220362_all.shtml [IT168 技术]随着.NET 4.0时代的到来,开发者越来越关注如何 ...