Python iterative BFS


  • 0
    class Solution(object):
        def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            level = [root] if root else []
            while level:
                q = []
                for node in level:
                    if node.left:
                        q.append(node.left)                
                    if node.right:
                        q.append(node.right)
                    node.left, node.right = node.right, node.left
                    
                level = q
    
            return root
    

    :levitate:


Log in to reply
 

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