My 44ms Python solution with recursion


  • 0
    L
    class Solution(object):
        def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            if root is not None and root.val is not None:
                t = root.left
                root.left = root.right
                root.right = t
                self.invertTree(root.left)
                self.invertTree(root.right)
                return root
    

  • 0
    X
      def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            if root and root.val:
                root.left,root.right=root.right,root.left
                self.invertTree(root.left)
                self.invertTree(root.right)
                return root
    

    change same code to make it More pythonic


  • 0
    L

Log in to reply
 

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