Given the root
of a binary tree, flatten the tree into a "linked list":
TreeNode
class where the right
child pointer points to the next node in the list and the left
child pointer is always null
.
Example 1:
Input: root = [1,2,5,3,4,null,6] Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [0] Output: [0]
The core challenge of this problem is to transform a binary tree into a linked list using the same TreeNode class. The transformation should follow a pre-order traversal, meaning we visit the root node first, then recursively visit the left subtree, and finally the right subtree. This problem is significant in scenarios where tree structures need to be linearized for easier traversal or storage.
To solve this problem, we can consider multiple approaches:
A naive approach would involve performing a pre-order traversal of the tree and storing the nodes in a list. Then, we can iterate through the list to reassign the right pointers to form the linked list. However, this approach requires additional space for storing the nodes, making it less optimal.
An optimized approach involves modifying the tree in place without using extra space. We can achieve this by recursively flattening the left and right subtrees and then reassigning the pointers to form the linked list. This approach ensures that we only use O(1) extra space.
Here is a step-by-step breakdown of the optimized algorithm:
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void flatten(TreeNode* root) {
if (!root) return;
// Recursively flatten the left and right subtrees
flatten(root->left);
flatten(root->right);
// Store the right subtree in a temporary variable
TreeNode* tempRight = root->right;
// Move the left subtree to the right and set the left pointer to null
root->right = root->left;
root->left = nullptr;
// Traverse to the end of the new right subtree
TreeNode* current = root;
while (current->right) {
current = current->right;
}
// Attach the original right subtree
current->right = tempRight;
}
};
The time complexity of this approach is O(n), where n is the number of nodes in the tree. This is because we visit each node exactly once. The space complexity is O(1) as we are modifying the tree in place without using any extra space.
Consider the following edge cases:
To test the solution comprehensively, consider the following test cases:
When approaching such problems, it is essential to understand the traversal order and how to manipulate pointers effectively. Practice similar problems to develop a deeper understanding of tree manipulations and recursive algorithms.
Flattening a binary tree into a linked list is a common problem that tests your understanding of tree traversals and pointer manipulations. By practicing and understanding the underlying concepts, you can efficiently solve similar problems.