Very simple solution using recursion C++


  • -1
    S

    It's actually quite a simple question

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

Log in to reply
 

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