Invert a binary tree.
Example:
Input:
4 / 2 7 / / 1 3 6 9
Output:
4 / 7 2 / / 9 6 3 1
code
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public TreeNode invertTree(TreeNode root) { 12 if(null == root){ 13 return null; 14 } 15 16 TreeNode left = invertTree(root.left); 17 TreeNode right = invertTree(root.right); 18 root.left = right; 19 root.right = left; 20 21 return root; 22 } 23 }