Problem statement

https://binarysearch.com/problems/Invert-Tree/

Solution

Equal to Leetcode 0226. Invert Binary Tree.

Complexity

It is O(n) for time and O(h) for space.

Code

class Solution:
    def solve(self, root):
        if not root: return None
        root.left, root.right = self.solve(root.right), self.solve(root.left)
        return root