WTF is "inverting a binary tree?". The smattering of search engine results points to some seemingly operation that basically generates garbage by destructively manipulating the tree into a DAG in which the leaves of the original tree are roots, which point downward the parents. The original root node is returned, and still points to its children. Unless you return some aggregate (e.g. list) of all the roots which are not direct children of the original root, or it is understood that something elsewhere holds reference to them, they are leaked.
Be that as it may, if the interviewers hand you a reasonably detailed specification of what "invert a binary tree" means and you can't whiteboard it, I don't see how you can expect the job.
If you're expected to know what it means, and get no hint, then that is just stupid. "Whiteboard up an implementation for something we refuse to specify, beyond citing its name."
I agree. I have no idea what "inverting a binary tree" means, or why I would want to do it, so I'd need some context from the interviewer. Chances are, I'd ask some very pointed questions about whether a binary tree is really the best underlying data structure for whatever the "inversion" process is intended to accomplish. The interview would probably go downhill from there.
My guess is that the potential leaking of some nodes is one of the points of the question. A lot of people would not notice it.
Note that in the original tree each node needs storage for two pointers. After inversion only one is needed. You can use that now unused storage to link together the multiple roots to solve the leak problem.
But note that only consumes the second pointer storage in the roots. Interior nodes end up with some free storage after inversion.
Since inversion is reversible, the binary tree and its inverse contain the same information. Does this mean we should only need one pointer's worth of storage in the binary tree nodes?
Is there something for binary trees similar to Knuth's xor trick for doubly linked lists?
If you invert the tree such that each node points to its parent, and there are no child pointers, you lose information. Namely, the order among children is lost. A given interior node still has two children as before, but it is not known which is the left child and which is the right child; there is just a set of up to two nodes which point to the same parent.
The binary tree inverse specifications/implementations I have looked at preserve the information by selectively using the left or right links. For instance, given:
P
/ \
L R
The inverse would be:
L R
\ /
P
Both children point to the parent. But the left child uses its right pointer, and the right child uses the left pointer. That's what preserves the information about which child is which.
Be that as it may, if the interviewers hand you a reasonably detailed specification of what "invert a binary tree" means and you can't whiteboard it, I don't see how you can expect the job.
If you're expected to know what it means, and get no hint, then that is just stupid. "Whiteboard up an implementation for something we refuse to specify, beyond citing its name."