

Invert a binary tree on LeetCode - ripitrust
https://leetcode.com/problems/invert-binary-tree/
Inspired by Max Howell&#x27;s tweet
======
de_Selby
The problem wasn't to just reverse the nodes, it was to re-sort it:

> @rogerdai16 to min-max the tree, ascending to descending.

[https://twitter.com/mxcl/status/608891015945170944](https://twitter.com/mxcl/status/608891015945170944)

~~~
SamReidHughes
I can't stand this. That's an equally intelligible description of the problem
as the original.

~~~
de_Selby
Well it's a 140 character description from someone who's probably feeling a
bit emotional. I'm sure he got a clearer problem description in the actual
interview.

------
mgraczyk
In fairness to Google, the question is pretty darn simple...

    
    
        def invertTree(self, root):
            if root is not None:
              tmp = root.left
              root.left = self.invertTree(root.right)
              root.right = self.invertTree(tmp)
            
            return root

~~~
danieldk
Indeed, it's not a difficult question.

I think people were making fun of Google, because it is a caricature of their
whole interview process.

If a large number of your engineers is using Homebrew, having its developer is
an asset for many different reasons (e.g. influencing the focus of the
project, getting formulae in that interest you, or simply improving a tool
that is important to your productivity). But no, they reject him because he
cannot invert a binary tree.

It's like throwing one of the better offenders out of your soccer team,
because he cannot substitute as a goalkeeper.

~~~
onion2k
Having the developer of Homebrew as an employee is only an advantage if you
want to influence it's development, and if the developer is happy for his
application to be influenced by his employer rather than the Homebrew
community as a whole.

In fact, I would see Google hiring the leaders of open source projects _in
order to influence the direction the project takes_ as a problem. Sponsoring
an open source project is brilliant, and Google are great at that, but buying
influence over it is completely different and (potentially) an issue that
would lead to people forking the project.

~~~
r0naa
If interviews are really made to maintain high-engineering standards and
prevent bad hires, interviewing and rejecting the author of a software that
90% of your employees use everyday is quite perplexing indeed.

While I understand that interviews are sometimes necessary to evaluate a
candidate, it seems completely out of touch for Google to pass on an applicant
with such an impressive background.

Not only that, but heaven knows that it takes a lot of dedication and
leadership to lead an open source project from ideation to wide-spread use. If
I were a startup founder, I would kill to get someone like him on my team.

------
meggar
This looks like it works

    
    
        <html>
        <style>
        pre {
            -webkit-transform:scaleX(-1);
            -moz-transform:scaleX(-1);
            -ms-transform:scaleX(-1);
            -o-transform:scaleX(-1);
            transform:scaleX(-1);
        }
        </style>
        <pre>
             4
           /   \
          2     7
         / \   / \
        1   3 6   9
        </pre>
        </html>

------
diminish
What's hard is to have necessary CS background to understand what "inverting a
tree" as a term means. As a non-CS major, I don't know what they use it for as
a "term" and it evokes several things I can do with a tree as an outsider. So
interviewers can quickly eliminate me as a non-CS. How to invert a tree on a
whiteboard? Use a mirror.

~~~
logicchains
I like the solution a friend suggested: flip the interviewer upside-down.

~~~
vishnuks
I would go for flipping the White Board.

------
cousin_it
At first I thought that he was given a slightly harder problem: given a binary
tree and a particular leaf node of that tree, return another tree that's
graph-isomorphic to the original, but has that node as the root. (You can
visualize that as picking up the tree by a leaf node and shaking it, then
reinterpreting the result as another tree.)

------
raverbashing
Too bad the website requires you to sign up to run your solution.

But it's an interesting concept, I've had to use a similar one in the past for
a job interview.

~~~
caf
There's a bugmenot login that works.

------
djhworld
I've enjoyed many of the responses on this topic, some people like to use it
as a self congratulatory platform to express their position that the solution
is trivial and "any engineer should be able to do it like me" with a piece of
example code, while others use it as a soapbox to moan about the scourge of
the tech interview process.

~~~
falcolas
I've noticed that dichotomy as well. It seems to be splitting along culture
lines - so one of these two camps would be right at home at Google, the other
would be fairly miserable.

Of course, this creates its own issues related to creating and maintaining a
mono-culture, but that's a topic for another thread.

------
AYBABTME
This topic has spilled too much ink.

~~~
falcolas
Its prevalence suggests that it's a pretty big issue that many interviewees
face, and nobody has the answer on which is the best approach for hiring
competent developers at scale.

------
ripitrust
C++ Solution here :

TreeNode* invertTree(TreeNode* root) { if (root) { TreeNode* temp =
root->left; root->left = invertTree(root->right); root->right =
invertTree(temp); } return root; }

------
wfunction
Considering the answer is 1 line, I doubt I would have hired him either...

    
    
        def inverse(t): return Node(inverse(t.r), t.v, inverse(t.l)) if t else t
    

or if you must do it in place, 3 lines...

    
    
        def invert(t):
            if t: (t.l, t.r) = (invert(t.r), t.v, invert(t.l))
            return t

------
ExpiredLink
>> _You have not signed in, cannot submit your code. </a>_

~~~
aaggarwal
You can use [http://ideone.com](http://ideone.com), it doesn't require you to
sign in to execute your code and allows almost every language.

------
adnanh
My JS

    
    
      var invertTree = function(root) {
          if (!root) return null;
    
          root.right = [invertTree(root.left), root.left = invertTree(root.right)][0];
    
          return root;
      };

------
jbrooksuk
As a non-CS major, what is inverting a binary tree used for? Google just keeps
bringing me back to Max's tweet, or blog posts about it.

~~~
jbrooksuk
I assume it's for sorting (and ordering) algorithms?

------
fishnchips
This is just flogging a dead horse.

