226 Invert Binary Tree
Problem
Solution
To invert a tree, just invert its left subtree and right subtree and then swap these two subtrees, another good example of recursion.
Code
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root != null) {
TreeNode left = invertTree(root.right);
TreeNode right = invertTree(root.left);
root.left = left;
root.right = right;
}
return root;
}
}