Flatten Binary Tree To Linked List

To solve this coding challenge, we need to understand the problem requirements and implement a solution that flattens a binary tree into a linked list using pre-order traversal. The right child pointer of each node should point to the next node in the list, and the left child pointer should be null.

# Explanation

The challenge involves manipulating a binary tree structure to transform it into a single, right-skewed list that reflects a pre-order traversal. Pre-order traversal means visiting the root node first, then recursively visiting the left subtree, followed by the right subtree.
Requirements:
  1. Modify the original tree in place (O(1) extra space).
  2. Ensure the resultant list represents a pre-order traversal.
  3. High-Level Approach:
  4. Traverse the tree using a stack to simulate the recursive behavior without using extra space inherent in recursion.
  5. Begin from the root node and process nodes in pre-order fashion.
  6. Maintain a stack to store nodes temporarily.
  7. For each node, visit the node, push its right child onto the stack (if it exists), then push its left child onto the stack (if it exists).
  8. Adjust the pointers such that the left child pointer of the current node is always null, and the right child pointer points to the next node in pre-order traversal.
  9. Step-by-Step Explanation:
  10. Initialization :
    • Check if the root is null. If null, there is nothing to flatten, so return immediately.
  11. Stack Setup :
    • Initialize a stack and push the root node onto it. This simulates the beginning of the pre-order traversal.
  12. Iteration and Node Processing :
    • While the stack is not empty, continue processing:
      • Pop the node from the stack, which represents the current node.
      • If the current node has a right child, push it onto the stack. This ensures the right child is processed after the left child but before siblings.
      • If the current node has a left child, push it onto the stack.
      • If the stack is not empty, adjust the current node's right pointer to point to the node on the top of the stack, ensuring the correct pre-order linkage.
      • Set the current node's left child to null.

# Detailed Steps in Pseudocode:

                                            
// Definition of TreeNode:
// class TreeNode {
//   value
//   left child (TreeNode)
//   right child (TreeNode)
// }

// Function to flatten the binary tree to a linked list
function flattenBinaryTreeToLinkedList(rootNode):
    // Base case: if the root node is null, return immediately
    if rootNode is null:
        return

    // Initialize a stack to keep track of nodes to process
    stack = create new stack
    stack.push(rootNode)

    // Process nodes until the stack is empty
    while stack is not empty:
        // Pop the top node from the stack as the current node to process
        currentNode = stack.pop()

        // If the current node has a right child, push it onto the stack
        if currentNode.rightChild is not null:
            stack.push(currentNode.rightChild)

        // If the current node has a left child, push it onto the stack
        if currentNode.leftChild is not null:
            stack.push(currentNode.leftChild)

        // If the stack is not empty, set the current node's right child to the next node in pre-order
        if stack is not empty:
            currentNode.rightChild = stack.top()

        // Set the current node's left child to null, as the left pointers are not used in the linked list format
        currentNode.leftChild = null

// Example use case to call the function:
// rootNode = [ Example Tree ]
// flattenBinaryTreeToLinkedList(rootNode)
// The tree rooted at 'rootNode' should now be flattened into a linked list

                                        
By following this detailed explanation and accompanying pseudocode, you will be able to transform a binary tree into a linked list following the pre-order traversal sequence. This method ensures in-place modification of the original tree without requiring additional space, as dictated by the follow-up constraint of the problem.