Problem J
A Night at Buckingham Palace
breaklines=true,
In a unfortunate turn of events, Sherlock Holmes’ assistant Dr. John Watson has died in an explosion while he was attending matters at the Buckingham Palace. Fortunately, the Royal family was not in town, so whoever planned the attack was clearly not very intelligent. Regardless, Mycroft, Sherlock’s brother and a “minor” member of the British Government, cares quite deeply about this attack and immediately asks Sherlock to start investigating. However, left assistant-less, he quickly recruits you to replace Dr. John Watson!
After looking through Buckingham Palace, Sherlock quickly determines that the attacker is Moriarty, and he has dropped direction cards (notecards with the exact directions to get to his location) all over the palace. These cards, if assembled correctly, will lead you straight to the Moriarty. Luckily, to assemble these cards, there are a few hints provided. On the back of the cards, Moriarty has written pairs in the format (n, m) where n is the value of the card, and m is a string of “L”s and “R”s. Based on this data, Sherlock has figured out that each card belongs to a binary tree, and the sequence of “L”s and “R”s determines where in the tree it is. If there is an empty string instead of “L”s and “R”s, that value is the root of the tree. Sherlock has also determined that to put together the direction cards, you must print each level of the binary tree (level order traversal), and the string of each level printed is the order of the direction cards that will lead you right to Moriarty.
As an example, in the above, the node containing 9 would be represented by (9,), and the node containing 8 would be represented by (8, RL). The level order traversal would be 9, 3, 1, 12, 7, 8, 14, 5, 10, 2, 6, 4, 11.
However, Moriarity may have taken a few cards with him, rendering the tree incomplete. For example, if the card (3, L) wasn’t present in the tree, then the tree would be incomplete and you would output “incomplete.”
![\includegraphics[width=0.5\textwidth ]{anightatbuckinghampalace.png}](/problems/anightatbuckinghampalace/file/statement/en/img-0001.png)
Input
The input consists of sequences of direction cards specified as described above. Each direction card has a pair ‘(n,m)’ as described above. Each sequence is terminated by ‘()’. All direction cards contain a positive integer. Every direction card sequence will consist of at least one card and no more than 256 cards.
Output
For each complete sequence in the input, the level order traversal of that sequence should be printed. If the sequence is incomplete, then output the string "incomplete."
Sample Input 1 | Sample Output 1 |
---|---|
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) () (3,L) (4,R) () |
5 4 8 11 13 4 7 2 1 incomplete |