# 114-flatten-binary-tree-to-linked-list Try it on leetcode ## Description

Given the root of a binary tree, flatten the tree into a "linked list":

 

Example 1:

Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [0]
Output: [0]

 

Constraints:

 

Follow up: Can you flatten the tree in-place (with O(1) extra space)?
## Solution(Python) ```Python # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def flatten(self, root: Optional[TreeNode]) -> None: """ Do not return anything, modify root in-place instead. """ cur = root while cur: if cur.left: pre = cur.left while pre.right: pre = pre.right pre.right = cur.right cur.right = cur.left cur.left = None cur = cur.right ```