Concise Morris traversal based solution in Python


  • 0
    P
    class Solution(object):
        def flatten(self, root):
            curr = root
            while curr:
                if curr.left:
                    prev = curr.left
                    while prev.right:
                        prev = prev.right
                        
                    prev.right = curr.right
                    curr.right = curr.left
                    tmp = curr.left; curr.left = None
                    curr = tmp
                else:
                    curr = curr.right
    
    

Log in to reply
 

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