Invert a binary tree.

Example:

Input:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode InvertTree(TreeNode root) {
        if(root == null)
            return root;
        InvertTreeHelper(root);
        return root;
    }
    
    public void InvertTreeHelper(TreeNode root)
    {
       if(root != null)
       {
           InvertTreeHelper(root.left);
           InvertTreeHelper(root.right);
           TreeNode tempNode = root.left;
           root.left = root.right;
           root.right = tempNode;
       }
        return;
    }
}