Leetcode226:Invert Binary Tree

Invert a binary tree.

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

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) 
{
    struct TreeNode *tmp;
    if(root == NULL)
    {
        return NULL;
    }
    tmp = root->left;
    root->left = invertTree(root->right);
    root->right = invertTree(tmp);
    return root;
    
}

 

posted @ 2016-03-02 21:03  EvansYang  阅读(166)  评论(0编辑  收藏  举报