1
# Runtime: 59 ms (Top 52.37%) | Memory: 15.1 MB (Top 89.03%)2
# Call the right of the tree node till the node root left and right is not None3
# After reaching the bottom of the tree make the root.right = prev and4
# root.left = None and then prev = None5
# Initially prev will point to None but this is used to point the previously visited root node6
# Prev pointer helps us to change the values from left to right8
def flatten(self, root: Optional[TreeNode]) -> None:10
Do not return anything, modify root in-place instead.12
prev = None # You can also define that variable inside the init function using self keyword30
# If the above solution is hard to understand than one can do level order traversal31
# Using Stack DS but this will increase the space complexity to O(N).