美文网首页
Invert a binary tree

Invert a binary tree

作者: 极速魔法 | 来源:发表于2017-07-02 11:05 被阅读3次

//226

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;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==NULL){
            return NULL;
        }
       root->left=invertTree(root->left);
        root->right=invertTree(root->right);
        swap(root->left,root->right);

        return root;
    }
};

相关文章

网友评论

      本文标题:Invert a binary tree

      本文链接:https://www.haomeiwen.com/subject/nkyncxtx.html