My simple c++ solution


  • 8
    B
    class Solution 
    {
    public:
    TreeNode* invertTree(TreeNode* root) 
        {
            if(root!=NULL)
            {
                TreeNode* tmp=root->left;
                root->left=root->right;
                root->right=tmp;
                invertTree(root->left);
                invertTree(root->right);
            }
            return root;
        }
    };

  • 0
    F
    This post is deleted!

  • 4
    F

    It can be further simplified as follows:

      TreeNode* invertTree(TreeNode* root) {
            if (!root)
                return nullptr;
            auto tmp = root->right;
            root->right = invertTree(root->left);
            root->left = invertTree(tmp);
            return root;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.