Apparently, the author of Homebrew couldn’t solve this problem while interviewing for a job at Google, I gave it a shot with the comfort a living room and no time pressure.
Problem:
https://leetcode.com/problems/invert-binary-tree/
Solution:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return root; TreeNode temp = root.left; root.left = root.right; root.right = temp; invertTree(root.right); invertTree(root.left); return root; } }