My simple Python solution


  • 2
    H
    class Solution(object):
        def flatten(self, root):
            """
            :type root: TreeNode
            :rtype: void Do not return anything, modify root in-place instead.
            """
            if not root:
                return root
            self.flattenHelper(root)
            
        def flattenHelper(self, root):
            if not root.left and not root.right:
                return root
            left, right = root.left, root.right
            end = root
            
            if left:
                root.left = None
                root.right = left
                end = self.flattenHelper(left)
            if right:
                end.right = right
                end = self.flattenHelper(right)
            
            return end

Log in to reply
 

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