Invert a binary tree.

Example:

 1234567891011121314151617 Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 

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 f*** off.

Related Topics: Hash Table

## 解題邏輯與實作

### 遞迴

 12345678910111213 class Solution: def invertTree(self, root): self.invert(root) return root def invert(self, root): if root is None: return self.invertTree(root.left) self.invertTree(root.right) root.left, root.right = root.right, root.left 

### 最速解

 1234 class Solution: def invertTree(self, root): print("(╯°□°)╯︵ ǝǝɹʇ ʎɹɐuıq") return root