Super intuitive recursive Python solution


  • 0
    S

    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:
                return
            root.left, root.right = root.right, root.left
            if root.left:
                self.invertTree(root.left)
            if root.right:
                self.invertTree(root.right)
            return root
    

Log in to reply
 

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