Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
4
/ \
7 2
/ \ / \
9 6 3 1
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
helper(root);
return root;
}
private void helper(TreeNode root) {
if (root == null) {
return;
}
helper(root.left);
helper(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}