First Thoughts: binary tree is inherently recursive. inverting left and right subtrees recursively will eventually result in inverting the whole tree.
My Solution:
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root==null) return null;
TreeNode leftInverted = invertTree(root.right);
TreeNode rightInverted = invertTree(root.left);
TreeNode inverted = new TreeNode(root.val, leftInverted, rightInverted);
return inverted;
}
}