One possible python solution


  • 0
    S
    class Solution(object):
        def flatten(self, root):  
            if not root:
                return
            
            if not root.right:
                root.right = root.left
            elif root.left:                
                tmp = root.left
                while tmp.right:
                    tmp = tmp.right
                tmp.right = root.right
                root.right = root.left
                
            root.left = None
            self.flatten(root.right)
    

    What makes the most sense to me. Whatever is currently in root.right comes after anything in root.left, so move all of root.right to the last spot of root.left. Then recursively repeat this process.


Log in to reply
 

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