My Iterative Solution, Very straightforward


  • 1
    L
    class Solution(object):
        def invertTree(self, root):
            """
            :type root: TreeNode
            :rtype: TreeNode
            """
            if not root:
                return root
            pre = root
            queue = [pre]
            while queue:
                current = queue.pop()
                if current.left:
                    queue.append(current.left)
                if current.right:
                    queue.append(current.right)
                current.left, current.right = current.right, current.left
            return pre

  • 0
    A

    It's interesting to use queue instead of recursive. How about efficiency in this approach?


  • 0
    L

    it's O(n). and I don't know if there is a faster solution?


Log in to reply
 

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