26 ms·
2 2 / \ / \ / \ / \ / \
by guessmyname 9y ago
2 2
/ \ / \
/ \ / \
/ \ / \
1 3 3 1
/ \ / \ / \ / \
0 7 9 1 reverse >>> 1 9 7 0
/ / \ / \ / \ / \ \
2 1 0 8 8 8 8 0 1 2
/ \
7 7
- hinkley 9y agoThat’s reversing a binary tree.
- guessmyname 9y agoInverting == Reversing [1] [1] https://leetcode.com/problems/invert-binary-tree/description/ https://leetcode.com/problems/invert-binary-tree/description...
- nathan_f77 9y agoThat was fun, thanks! I remember reading that tweet a while ago, and "inverting a binary tree" sounded very complicated, but it's really just one line of code.
- hinkley 9y agoThis problem was inspired by this original tweet by Max Howell There are many definitions of invert. This is relying on one definition that is jargon, and not widely agreed upon jargon at that. For a two or more dimensional “object”. Invert means to reorient the top and the bottom, while reverse swaps the left and the right. The two only might mean the same thing with one dimensional data structures. For instance inverting a line of text might (ambiguously) mean reversing it. Or drawing it upside down. Or upside down and backward. But at least you would expect a clarification.
- mullen 9y agoWhy would you want to reverse/inverse a binary tree?
- sushid 9y agoIt's an easy way to check if you can think recursively/are familiar with a btree.
- tomp 9y agoAh, I would personally call that "flipping", I kind-of imagined that "inverting" would mean maybe taking a node and putting in the root of the tree or something similar...