Python Recursion with Detailed Explanations
This is similar to how we convert a decimal number to its binary form. A simple example is when N = 9. The following is how we convert 9 to 1001 with the base o
This is similar to how we convert a decimal number to its binary form. A simple example is when `N = 9`. The following is how we convert `9` to `1001` with the base of `2`.\n9 = 2 * 4 + **1**\n4 = 2 * 2 + **0**\n2 = 2 * 1 + **0**\n1 = 2 * 0 + **1**\nEach time we decompose a number using `N = 2 * q + r`, it\'s like doing `bin(N) = bin(q) + str(r)`. Reversely (from the bottom-up perspective), `1` is `0b1`; `2` is `0b10`; `4` is `0b100`; `9` is `0b1001`.\nWhen the base is `-2`, it is the same logic.\n9 = -2 * -4 + **1**\n-4 = -2 * 2 + **0**\n2 = -2 * -1 + **0**\n-1 = -2 * 1 + **1**\n1 = -2 * 0 + **1**\nTherefore, `9` is converted to `11001`, the reverse of `10011`.\n```\nclass Solution:\n    def baseNeg2(self, N: int) -> str:\n        if N in [0, 1]: return str(N)\n        if N % 2 == 0:\n            return self.baseNeg2(N // -2) + \'0\'\n        else:\n            return self.baseNeg2((N - 1) // -2) + \'1\'\n```
['Recursion', 'Python']