Solution: Flatten Binary Tree to Linked List
Explore how to flatten a binary tree into a linked list by rearranging its nodes in preorder sequence using depth-first search. Understand an optimized method that modifies the tree in place, avoiding extra memory use, and learn the step-by-step process to implement this solution. This lesson helps solidify your grasp of tree traversal techniques and efficient problem-solving strategies.
Statement
Given the root of a binary tree, the task is to flatten the tree into a linked list using the same TreeNode class. The left child pointer of each node in the linked list should always be NULL, and the right child pointer should point to the next node in the linked list. The nodes in the linked list should be in the same order as that of the preorder traversal of the given binary tree.
Constraints:
-
Node.data.