32ms 5 Lines - Python Code


  • 1
    R
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            left, right = None, None
            
            if not root:    return root  
            if root.left:   left = self.invertTree(root.left)
            if root.right:  right = self.invertTree(root.right)
                
            root.left, root.right = right, left
            
            return root 
            
    

Log in to reply
 

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