My 0ms C++ clean-code solution


  • 0
    J
    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            
            if (root == NULL)
            {
                return root;
            }
            
            if (root->left == NULL && root->right == NULL)
            {
                return root;
            }
            else{
                TreeNode *tmp = root->left;
                root->left = root->right;
                root->right = tmp;
                
                invertTree(root->left);
                invertTree(root->right);
            }
            
            return root; 
        }
    };

Log in to reply
 

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