C++ DFS recursive easy and fast, but 10 lines


  • 0
    S
    TreeNode* invertTree(TreeNode* root) {
        if(root ==NULL)
            return root;
        
        if(root->left!=NULL)
            invertTree(root->left);
        if(root->right!=NULL)
            invertTree(root->right);
        
        TreeNode* temp =root->left;
        root->left = root->right;
        root->right=temp;
        
        return root;
    }

Log in to reply
 

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