Flatten Binary Tree to Linked List

LeetCode 114

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def __init__(self):
        self.tmp = None
        
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: void Do not return anything, modify root in-place instead.
        """
                
        if not root:
            return
        self.flatten(root.right)
        self.flatten(root.left)

        root.right = self.tmp
        root.left = None
        self.tmp = root

Last updated