POJ 1577 Falling Leaves

Description


Figure 1 shows a graphical representation of a binary tree of letters. Peoplefamiliar with binary trees can skip over the definitions of a binary tree ofletters, leaves of a binary tree, and a binary search tree of letters, and goright 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 examplein 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. Abinary search tree of letters is a binary tree of letters in which each nodesatisfies:

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

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

The problem:

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

Remove the leaves and list the data removed
Repeat this procedure until the tree is empty

Starting from the tree belowon 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 binarysearch tree of letters and output the preorder traversal of the tree.

Input

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

The lines contain the leaves removed from a binary search tree in the stagesdescribed above. The letters on a line will be listed in increasingalphabetical order. Data sets are separated by a line containing only anasterisk ('*').

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, thereis a unique binary search tree that would produce the sequence of leaves. Theoutput is a line containing only the preorder traversal of that tree, with noblanks.

Sample Input

BDHPY

CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

 

题目简介:给一些字母,这些字母是放在一棵二叉树上的。

这些字母的顺序是从左往右,从下到上给的。第一行表示每个分枝最下面一个字母,即树叶。第二行即每个分枝倒数第二个。以此类推。输出是从根节点开始先左再右。每个节点在第一次遇见就输出。

方法:是一个简单的二叉建树的问题。我的做法是,先将按输入顺序保存,接着从最后一个向前建树。然后再按照二叉树遍历的顺序输出就完了。我记得我的同学还有一个比较简单的方法,不过忘了。。。。。。。

PS:那几张图不知为何不能放上来。。。。。

你可能感兴趣的:(POJ 1577 Falling Leaves)