题目:
Invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9to
4 / \ 7 2 / \ / \ 9 6 3 1Trivia:
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.题解:
首先要说:傻叉谷歌,fuck off,又朝一日一定拿到你的offer再甩在你的脸上。
C++版:
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return NULL; TreeNode* temp = root->left; root->left = root->right; root->right = temp; invertTree(root->left); invertTree(root->right); return root; } };
public class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return null; TreeNode temp = root.left; root.left = root.right; root.right = temp; invertTree(root.left); invertTree(root.right); return root; } }
class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root == None: return None temp = root.left root.left = root.right root.right = temp self.invertTree(root.left) self.invertTree(root.right) return root