12-line Python In-Place solution


  • 4
    B
    class Solution:
        def flatten(self, root):
            if not root:
                return
            self.flatten(root.right)
            if not root.left:
                return
            self.flatten(root.left)
            tail = root.left
            while tail.right:
                tail = tail.right
            tail.right = root.right
            root.right = root.left
            root.left = None

  • 0

    It seems O(N^2)?


Log in to reply
 

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