###########################################################################
#                                                                         #
# Author: Daniel Mock                                                     #
#                                                                         #
# Purpose: To document progress in learning algorithms & data structures  #
#                                                                         #
# References: This question was generated by leetcode.com. The solution   #
# to the question was generated by Daniel Mock.                           #
#                                                                         #
###########################################################################

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

class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        stack = []

        if root:
            if root.left:
                self.flatten(root.left)
                tmp = root.right
                root.right = root.left
                root.left = None
                node = root.right
                while node.right:
                    node = node.right
                node.right = tmp
            self.flatten(root.right)





        '''
        if root is None: return root
        stack = self.recurse(root, [])
        node = stack.pop(0)
        ref = node
        while len(stack) > 0:
            node.right = stack.pop(0)
            node = node.right
        root = ref
        while(ref):
            print(ref.val)
            print(ref.left)
            ref = ref.right


    def recurse(self, root, stack):
        if root is not None:
            node = TreeNode(root.val)
            stack.append(node)
            node = node.right
        else:
            node = None
            return stack
        self.recurse(root.left, stack)
        self.recurse(root.right, stack)

        return stack
        '''
