Description:
Invert a binary tree.
Solution:
敲黑板,重点:homebrew创始人(90%谷歌员工用它写代码),去谷歌面试,结果不能在白板上写出翻转二叉树,然后喜提Rej
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.
先是递归算法:
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
再是遍历算法:
在这里插入代码片