Unit 9 Session 1 (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?
hotel
is None
)?
HAPPY CASE
Input:
hotel = Room("👻", Room("😱", Room("💀"), Room("😈")), Room("🧛🏾♀️"))
Output: [['💀', '😈', '🧛🏾♀️'], ['😱'], ['👻']]
Explanation:
* The tree is purged level by level, with the leaf nodes being collected and removed first.
Input:
hotel = Room("👻", Room("😱"), Room("🧛🏾♀️"))
Output: [['😱', '🧛🏾♀️'], ['👻']]
Explanation:
* The tree is purged, and the leaf nodes "😱" and "🧛🏾♀️" are collected first, followed by the root "👻".
EDGE CASE
Input: hotel = None
Output: []
Explanation: The tree is empty, so return an empty list.
Input: hotel = Room("👻")
Output: [['👻']]
Explanation: The tree has only one node, so collect the single leaf node "👻" and return it.
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 problems involving repeatedly removing leaf nodes from a binary tree, we can consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
1) Base Case:
hotel
is empty (None
), return an empty list since there are no rooms to purge.
2) Recursive Function:True
if the node is a leaf, so that it can be removed.Pseudocode:
1) If `hotel` is `None`, return an empty list.
2) Initialize an empty list `result` to store the collections of leaf nodes.
3) Define a recursive function `collect_leaves(node, parent, is_left)`:
a) If the node is `None`, return `False`.
b) If the node is a leaf, add its value to the `leaves` list, disconnect it from its parent, and return `True`.
c) Recur on the left and right children.
d) Return `False` after processing the children.
4) While the tree is not empty:
a) Initialize an empty list `leaves` to store the current collection of leaf nodes.
b) Call `collect_leaves` starting with the root node.
c) Append the `leaves` list to `result`.
5) Return the `result` list.
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def purge_hotel(hotel):
def collect_leaves(node, parent, is_left):
if not node:
return False
if not node.left and not node.right: # Node is a leaf
leaves.append(node.val)
if parent: # Disconnect this node from its parent
if is_left:
parent.left = None
else:
parent.right = None
return True
# Recurse on left and right children
collect_leaves(node.left, node, True)
collect_leaves(node.right, node, False)
return False
result = []
while hotel:
leaves = []
if collect_leaves(hotel, None, False): # If the root is a leaf
hotel = None # Entire tree is a single node, set to None
result.append(leaves)
return result
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
hotel = Room("👻", Room("😱", Room("💀"), Room("😈")), Room("🧛🏾♀️"))
:
result
.Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(N^2)
in the worst case, as each node may need to be revisited multiple times while collecting and removing leaves.O(N)
due to the recursive call stack and the storage of leaf nodes in the result
list.