2015年7月3日星期五

LeetCode: Invert Binary Tree



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==NULL) return root;
        invert(root);
        return root;
    }
    void invert(TreeNode* node){
        if(node->left == NULL && node->right == NULL) return;
        TreeNode* temp;
        temp=node->left;
        node->left = node->right;
        node->right =temp;
        if(node->left !=NULL) invert(node->left);
        if(node->right !=NULL) invert(node->right);
    }
};

没有评论:

发表评论