TIP102 Unit 9 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
None
, and right pointers represent the linked list.HAPPY CASE
Input:
items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", None, "Blondies"]
Output:
['Croissant', None, 'Cupcake', None, 'Cake', None, 'Pie', None, 'Bagel', None, 'Blondies']
Explanation:
The tree structure is flattened to a linked list:
Croissant
\
Cupcake
\
Cake
\
Pie
\
Bagel
\
Blondies
EDGE CASE
Input:
items = ["Croissant"]
Output:
['Croissant']
Explanation:
The tree has only one node, so the linked list is just that node.
Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Tree Flattening problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
1) Define a recursive function `flatten_orders(node)`:
- If `node` is `None`, return immediately.
- Recursively flatten the left and right subtrees.
- Store the original right subtree.
- Set the right subtree of the current node to be the flattened left subtree.
- Set the left pointer of the current node to `None`.
- Traverse to the end of the new right subtree.
- Attach the original right subtree to the end of the new right subtree.
⚠️ Common Mistakes
None
.Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def flatten_orders(orders):
# Base Case: If the node is None, return immediately
if not orders:
return
# Flatten the left and right subtrees
flatten_orders(orders.left)
flatten_orders(orders.right)
# Store the right subtree
right_subtree = orders.right
# Place the left subtree as the right subtree
orders.right = orders.left
orders.left = None
# Traverse to the end of the new right subtree (originally the left subtree)
current = orders
while current.right:
current = current.right
# Attach the original right subtree
current.right = right_subtree
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", None, "Blondies"]`
- Execution:
- Flatten the tree into a linked list using preorder traversal.
- Traverse and adjust the pointers accordingly.
- Output:
['Croissant', None, 'Cupcake', None, 'Cake', None, 'Pie', None, 'Bagel', None, 'Blondies']
- Example 2:
- Input:
`items = ["Croissant"]`
- Execution:
- Flatten the tree into a linked list.
- Output:
['Croissant']
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of nodes in the tree.
O(H)
where H
is the height of the tree.
O(N)
where N
is the number of nodes in the tree.