Super intuitive recursive Python solution

  • 0

    I found the recursive solution in the article hard to visualize. This way felt more intuitive to understand for me:

    Swap left and right of a node, then recursively do the same to its left and right childern.

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

Log in to reply

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