Flatten a binary tree into linked listIntroductionEach node in a binary tree has a maximum of two children, known as the left child and the right child, making it a hierarchical data structure. In order to convert a binary tree into a linked list, the tree nodes must be rearranged in a certain order to make a linear structure. This process is known as "flattening." The objective is to rearrange the nodes of a binary tree so that the tree's structure is maintained while creating a linked list. It is important to create the linked list in a certain order to facilitate simple traversal. Method 1: RecursionUsing a depth-first traversal approach, the recursive method flattens a binary tree into a linked list by iteratively moving through the tree structure. The function looks for a base case at every stage, making sure to stop when a null node is detected. The approach maintains a reference to the right subtree before flattening the left subtree, which is essential for subsequent reattachment. The procedure is then repeated via a recursive call on the left subtree. The function flattens the left subtree and then moves it to the right of the current node by adjusting pointers. It flattens the original right subtree recursively and proceeds to the end of this new flattened subtree on the right. Because it is recursive, the whole binary tree is inspected, and the nodes are rearranged in a particular sequence to create a linked list. Recursion is elegant because it can decompose a complicated problem into smaller, more manageable subproblems, resulting in more concise and expressive code. Code Output: Code Explanation Binary Tree Node Definition
Flatten Function
Base Case Verification
Save Right Subtree
Left Flattening Subtree
Transfer the Left Flattened Subtree to the Right
Proceed to the Right, to the End of the Flattened Subtree
Original Right Subtree Flattening
Join the End with the Flattened Right Subtree
Print Linked List Function
Method 2: StackThis approach uses an explicit stack to track the nodes as it traverses the graph. The root node is pushed into an empty stack that the algorithm has initialized. A node is removed from the stack at each iteration of a while loop, which runs until the stack is empty. If there is a stack, the right kid is pushed onto it first, and then the left child. Processing of the left child comes first, thanks to this arrangement. The technique simultaneously modifies pointers to link the current node to the appropriate child. The binary tree is flattened depth-first by explicitly managing the traversal order using the stack and modifying pointers accordingly. When recursion is undesirable, this approach is especially helpful because the explicit stack offers an organized approach to handle the traversal and flattening process sequentially. Code Output: Code Explanation Definition of a Binary Tree Node
Definition of Stack Node
Definition of Stack
Initialization of Stack
Empty Stack Check
Push Mechanism
Pop Function
Flattening Mechanism
Method 3: Iterative without StackThis technique implicitly simulates the functionality of a stack using a threaded binary tree architecture. It makes use of the Morris Traversal tree traversal method, which threads each node's rightmost child to its successor in the in-order traversal sequence, therefore changing the tree topology. The Morris Traversal is modified for flattening purposes so that it may traverse and flatten the tree at the same time, doing away with the requirement for a stack or other extra data structure. The algorithm moves through the tree quickly and accurately by creating threaded connections between nodes and modifying pointers along the way. This technique is distinguished by its constant space complexity, which renders it a memory-conserving and effective substitute for both recursive and stack-based methods. Code Output: Code Explanation Definition of a Binary Tree Node
Flatten Function
Main Function
Deallocation of Memory The allocated memory for the flattened linked list and the binary tree nodes is released by the while loop in the main function. Every node in the linked list is released as it moves through it. |