simple Python with DFS


  • 0
    def invertTree(self, root):
        if not root:
            return None
        stack = [root]
        while(stack):
            node = stack.pop()
            if node:
                node.left, node.right = node.right, node.left
                stack.append(node.left)
                stack.append(node.right)
        return root

Log in to reply
 

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