MP
BAN USER- -1of 1 vote
AnswersFrom a binary tree the leaf nodes are removed to get another tree and then leaf nodes are removed from that tree also. The process is continued till all the nodes of the tree are removed.
- MP in India
Given a set of inputs where each line contains the leaf nodes removed in a iteration. Print the pre-order traversal of the binary tree.
For example:
BDHPY
CM
GQ
K
Output would be:
KGCBDHQMPY| Report Duplicate | Flag | PURGE
Amazon Senior Software Development Engineer Algorithm
@ Arunkumar I myself got a different pre-order. And this is the complete question. No other hint (apart from the example itself).
@ Anonymous: That's the only test case given!
Though, while discussing with one of my friends we got an approach that can produce the same pre-order given in the example.
1. pop left most character in line, add it to first leaf from left.
2. pop right most character in line, add to first leaf from right.
This way you add first child in all leaf nodes, before adding their second child.
Though in the absence of additional test cases, its all speculative. Hope someone has a better answer.
Today, written online test.
- MP June 01, 2013