#BST0000014. Liệt kê lá theo thứ tự giảm dần (List BST Leaves in Descending Order)
Liệt kê lá theo thứ tự giảm dần (List BST Leaves in Descending Order)
List BST Leaves in Descending Order
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in insertion order. Duplicate keys are ignored.
A node is a leaf if it has neither a left child nor a right child. In a one-node tree, the root itself is a leaf.
Collect the keys of all leaf nodes and print them in strictly descending order.
Input
The first line contains .
If , the next line contains signed integers . If , this line is absent.
Output
If the tree has at least one leaf, print all leaf keys in descending order on one line, separated by one space.
If the tree is empty, print EMPTY.
Subtasks
Subtask 1 (20 points): .
Subtask 2 (30 points): .
Subtask 3 (50 points): .
In all subtasks, . Duplicate insertion keys are ignored.
Examples
Input
7
8 3 10 1 6 14 4
Output
14 4 1
Explanation
In the constructed tree, the nodes with keys , , and have neither a left child nor a right child, so these are exactly the leaf nodes. Every other node has at least one child.
The leaf keys are . In descending order they are , so the program prints 14 4 1.