nvert a binary tree. 4 / 2 7 / / 1 3 6 9 to 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 fuck off.
Of course use Recursion, on each level, have the root.left switch to right and root.right switch to left
1 public class Solution { 2 public TreeNode invertTree(TreeNode root) { 3 if (root == null) return null; 4 TreeNode left = root.left; 5 root.left = invertTree(root.right); 6 root.right = invertTree(left); 7 return root; 8 } 9 }