Recursive c++ solution (4ms), easy to understand


  • 0
    Z
    class Solution {
    void swapTwoBranches(TreeNode* root)
    {
        if(root!=NULL)
          if(root->left!=NULL||root->right!=NULL)
        {
            TreeNode* originLeftNode;
            originLeftNode = root->left;
            root->left = root->right;
            root->right = originLeftNode;
            
            swapTwoBranches(root->left);
            swapTwoBranches(root->right);
        }
    }
    
    TreeNode* invertTree(TreeNode* root) {
        
        swapTwoBranches(root);
        return root;
        
    }
    

    };


Log in to reply
 

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