Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
4
/ \
7 2
/ \ / \
9 6 3 1
Using recursion.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
invert(root);
return root;
}
public TreeNode invert(TreeNode node) {
if (node == null) {
return null;
}
TreeNode right = node.right;
node.right = invert(node.left);
node.left = invert(right);
return node;
}
}