One possible python solution

  • 0
    class Solution(object):
        def flatten(self, root):  
            if not root:
            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

    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.