Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

seconding this, creating value is orthogonal to knowing CS. see the creator of homebrew, Max Howell infamously failing to invert a binary tree in an interview: https://mobile.twitter.com/mxcl/status/608682016205344768?la...


I have yet to see a definition of what “invert a binary tree” even means.


I think "mirror" is a more intuitive description of the problem, but essentially you reverse all of the left and right subtrees. This Quora question [1] has a few different approaches listed.

[1]: https://www.quora.com/What-is-the-algorithmic-approach-to-in...


That doesn't sound like "inverting". If that is what it means then it is indeed a bad name.

I always thought it means making one of the leaf nodes a root of the tree. Physically, it would look like taking one of the leaf nodes with your fingers and "hanging" the tree off of it.


Inverting means inverting the sort order. The easy answer is to encapsulate the tree in a new structure and add a Boolean flag called “inverted,” since you’re just reinterpreting the meaning of left and right children.

What you describe is called “rotation,” and is used to rebalance.


invert: to reverse in position, order, or relationship


I'm confused. Doesn't the operation described in those answers just rename each node's properties, but leave the topology of the data structure unchanged?

Meaning one could just have the code accessing the tree swap which names it accesses, and the tree would behave as "inverted" with no actual need to invert anything?


It's when you swap the left and right branches of every node, e.g.:

    def invert(node):
        if node is not None:
            left = invert(node.left)
            right = invert(node.right)
            node.left = right
            node.right = left
        return node


Or simply

    node.left, node.right = map(invert, (node.right, node.left))


Coalescing those four lines onto one is fine, but writing:

    map(invert, (node.right, node.left))
is definitely not simpler than:

    invert(node.right), invert(node.left)


Indeed - I just wanted to write it in a form that could easily be mapped to languages which don't support multiple simultaneous assignments :)


I read it, tried it out and it didn't took me long to understand and solve that issue.

If you hack around for a long time and you never ever felt the need or motivation to understand very simple tree algorithms, okay. But don't rant when a company like google expects that from you.

He also answered, after a few years on a website about it:

https://www.quora.com/Whats-the-logic-behind-Google-rejectin...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: