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 we need to serialize a tree structure into a linear form for easier traversal or storage.
Potential pitfalls include not maintaining the pre-order traversal order and not correctly setting the left child pointers to null.
To solve this problem, we can use a recursive approach to perform a pre-order traversal and modify the tree in place. Here are the steps:
We can also use an iterative approach with a stack to simulate the pre-order traversal and modify the tree accordingly.
Here is a step-by-step breakdown of the recursive algorithm:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
if (root == null) return;
// Flatten the left and right subtrees
flatten(root.left);
flatten(root.right);
// Store the right subtree
TreeNode tempRight = root.right;
// Move the left subtree to the right
root.right = root.left;
root.left = null;
// Traverse to the end of the new right subtree
TreeNode current = root;
while (current.right != null) {
current = current.right;
}
// Attach the previously stored 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(h), where h is the height of the tree, due to the recursion stack.
Potential edge cases include:
Each of these cases is handled by the algorithm as it checks for null nodes and processes each node individually.
To test the solution comprehensively, consider the following test cases:
Use a testing framework like JUnit to automate these tests and ensure the correctness of the solution.
When approaching such problems, consider the following tips:
Flattening a binary tree into a linked list is a common problem that tests your understanding of tree traversal and in-place modification. By following the recursive approach outlined above, you can efficiently solve this problem with a time complexity of O(n). Practice and explore further to strengthen your problem-solving skills.
For further reading and practice, consider the following resources: