• 51.8%

https://leetcode.com/problems/invert-binary-tree/#/description

Trivia:

This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

BFS，层序遍历的方法，注意，要把层序当做先序，中序，后续遍历一样普通和容易想到的方法。

https://discuss.leetcode.com/topic/16138/recursive-and-non-recursive-c-both-4ms/3

Recursive and non-recursive C++ both 4ms

Recursive

Non-Recursive

https://discuss.leetcode.com/topic/16062/3-4-lines-python/4

3-4 lines Python

Maybe make it four lines for better readability:

And an iterative version using my own stack:

https://discuss.leetcode.com/topic/21271/python-solutions-recursively-dfs-bfs

Python solutions (recursively, dfs, bfs).

https://discuss.leetcode.com/topic/16039/straightforward-dfs-recursive-iterative-bfs-solutions/3

Straightforward DFS recursive, iterative, BFS solutions

As in many other cases this problem has more than one possible solutions:

Lets start with straightforward - recursive DFS - it’s easy to write and pretty much concise.

The above solution is correct, but it is also bound to the application stack, which means that it’s no so much scalable - (you can find the problem size that will overflow the stack and crash your application), so more robust solution would be to use stack data structure.

Finally we can easly convert the above solution to BFS - or so called level order traversal.

If I can write this code, does it mean I can get job at Google? ;)