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?
HAPPY CASE
Input:
inventory_items = ["Bread", "Croissant", "Donut", None, None, "Bagel", "Tart"]
Output:
[['Croissant'], ['Bread', 'Bagel'], ['Donut'], ['Tart']]
Explanation:
The tree structure:
Bread
/ \
Croissant Donut
/ \
Bagel Tart
The vertical order traversal results in four columns: [['Croissant'], ['Bread', 'Bagel'], ['Donut'], ['Tart']].
EDGE CASE
Input:
inventory_items = []
Output:
[]
Explanation:
The tree is empty, so the output is an empty list.
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 Vertical Order Traversal problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
1) Initialize a dictionary `column_table` to store nodes by their column index.
2) Initialize a queue for BFS that stores pairs `(node, column)` where `column` is the column index of the node.
3) While the queue is not empty:
- Dequeue the front of the queue.
- If the node is not `None`, add its value to `column_table[column]`.
- If the node has a left child, enqueue it with `column - 1`.
- If the node has a right child, enqueue it with `column + 1`.
4) After BFS completes, sort `column_table` by its keys (column indices).
5) Return a list of lists containing the node values in each column, ordered by column index.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
from collections import deque
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def vertical_inventory_display(root):
if not root:
return []
# Dictionary to hold lists of nodes by their column index
column_table = {}
# Queue to hold nodes along with their column index
queue = deque([(root, 0)])
while queue:
node, column = queue.popleft()
if node:
# If the column is not already in the dictionary, initialize it with an empty list
if column not in column_table:
column_table[column] = []
column_table[column].append(node.val)
# If there is a left child, it goes to column - 1
queue.append((node.left, column - 1))
# If there is a right child, it goes to column + 1
queue.append((node.right, column + 1))
# Sort columns and prepare the final output
sorted_columns = sorted(column_table.keys())
return [column_table[col] for col in sorted_columns]
# Example Usage:
inventory_items = ["Bread", "Croissant", "Donut", None, None, "Bagel", "Tart"]
inventory1 = build_tree(inventory_items)
inventory_items = ["Bread", "Croissant", "Donut", "Muffin", "Scone", "Bagel", "Tart", None, None, "Pie", None, "Cake"]
inventory2 = build_tree(inventory_items)
print(vertical_inventory_display(inventory1)) # [['Croissant'], ['Bread', 'Bagel'], ['Donut'], ['Tart']]
print(vertical_inventory_display(inventory2)) # [['Muffin'], ['Croissant', 'Pie'], ['Bread', 'Scone', 'Bagel'], ['Donut', 'Cake'], ['Tart']]
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`inventory_items = ["Bread", "Croissant", "Donut", None, None, "Bagel", "Tart"]`
- Execution:
- Perform BFS, group nodes by column index.
- Sort the columns and prepare the final output.
- Output:
[['Croissant'], ['Bread', 'Bagel'], ['Donut'], ['Tart']]
- Example 2:
- Input:
`inventory_items = ["Bread", "Croissant", "Donut", "Muffin", "Scone", "Bagel", "Tart", None, None, "Pie", None, "Cake"]`
- Execution:
- Perform BFS, group nodes by column index.
- Sort the columns and prepare the final output.
- Output:
[['Muffin'], ['Croissant', 'Pie'], ['Bread', 'Scone', 'Bagel'], ['Donut', 'Cake'], ['Tart']]
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N log N)
where N
is the number of nodes in the tree.
O(N log N)
.O(N)
where N
is the number of nodes in the tree.
O(N)
where N
is the number of nodes in the tree.