Python solution with detailed explanation


  • 0
    G

    Solution

    Invert Binary Tree https://leetcode.com/problems/invert-binary-tree/?tab=Description

    • Postorder - invert left and right sub-trees. Then swap left and right sub-trees.
    class Solution(object):
        def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            self.invert(root)
            return root
        
        def invert(self, root):
            if root == None:
                return
            self.invert(root.left)
            self.invert(root.right)
            root.left, root.right = root.right, root.left
            return
    

Log in to reply
 

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