题目:
Description
Figure 1
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
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.
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 <iostream>
#include <cstring>
using namespace std;
const int N = 100000+10;
struct Node {
char val;
Node *lch;
Node *rch;
};
char str[N];
Node* insertNode(Node *p, char ch) {
if (p == NULL) {
Node *q = new Node();
q->val = ch;
q->lch = q->rch = NULL;
return q;
}
else {
if (ch < p->val) p->lch = insertNode(p->lch, ch);
else p->rch = insertNode(p->rch, ch);
return p;
}
}
void preVisit(Node *p) {
if (p) {
if (p->val != NULL) cout << p->val;
preVisit(p->lch);
preVisit(p->rch);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
char ch;
int i = 0;
while (cin >> ch) {
if (ch != '*' && ch != '$') {
str[i] = ch;
++i;
}
else {
Node *root = new Node();
//for (int j = i-1;j >= 0;--j) {
// cout << str[j];
//}
//cout << endl;
for (int j = i-1;j >= 0;--j) {
insertNode(root, str[j]);
}
preVisit(root);
cout << endl;
delete root;
i = 0;
memset(str, 0, sizeof(str));
if (ch == '$') break;
}
}
return 0;
}
这道题虽然解法就是建立二叉搜索树然后进行先序遍历,但实际上无论是对题目的理解还是对解法的理解,都是有一点儿难度的。还得再研究研究。