Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9to
4
/ \
7 2
/ \ / \
9 6 3 1Trivia:
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.
1 | Subscribe to see which companies asked this question |
这到题目不难,主要用了递归的思想,反转二叉树就是每个节点下的左右儿子,我们需要从他的最下面开始反转,这里用递归就很合适。