How to find siblings of a tree?
- by smallB
On my interview for an internship, I was asked following question:
On a whiteboard write the simplest algorithm with use of recursion which would take a root of a so called binary tree (so called because it is not strictly speaking binary tree) and make every child in this tree connected with its sibling.
So if I have:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
then the sibling to 2 would be 3, to four five, to five six and to seven eight.
I didn't do this, although I was heading in the right direction. Later (next day) at home I did it, but with the use of a debugger. It took me better part of two hours and 50 lines of code. I personally think that this was very difficult question, almost impossible to do correctly on a whiteboard.
How would you solve it on a whiteboard? How to apprehend this question without using a debugger?