Maximum Tiers in Cake - codepath/compsci_guides GitHub Wiki
Unit 9 Session 1 (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 15 mins
- 🛠️ Topics: Binary Trees, Recursion, Depth-First Search
1: U-nderstand
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?
- What should be returned if the
cake
tree isNone
?- Return
0
since there are no tiers in the cake.
- Return
- What if the tree has only one node?
- Return
1
since the maximum number of tiers is 1.
- Return
- Is the tree guaranteed to be balanced?
- The problem assumes the input tree is balanced when calculating time complexity.
HAPPY CASE
Input: cake_sections = ["Chocolate", "Vanilla", "Strawberry", None, None, "Chocolate", "Coffee"]
Output: 3
Explanation: The longest path is from "Chocolate" -> "Strawberry" -> "Coffee".
Input: cake_sections = ["Vanilla"]
Output: 1
Explanation: The tree has only one node, so the maximum number of tiers is 1.
EDGE CASE
Input: cake_sections = []
Output: 0
Explanation: The tree is empty, so return 0.
Input: cake_sections = ["Chocolate", "Vanilla", "Strawberry", None, "Raspberry"]
Output: 3
Explanation: The longest path is from "Chocolate" -> "Vanilla" -> "Raspberry".
2: M-atch
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 finding the maximum depth of a binary tree, we can consider the following approaches:
- Depth-First Search (DFS): Use DFS to explore each branch of the tree and calculate the depth by returning the maximum depth found.
- Recursion: Recursively calculate the depth of the left and right subtrees and return the greater of the two plus one for the current node.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
Plan
Note: Like many interview questions, this problem can be solved multiple ways. If you used an iterative, BFS approach, take a look at the Maximum Tiers in Cake II solution.
- Base Case:
- If the
cake
tree is empty (None
), return0
.
- If the
- Recursive Depth Calculation:
- Recursively calculate the depth of the left subtree.
- Recursively calculate the depth of the right subtree.
- Return the greater of the two depths plus one for the current node.
Recursive Implementation
Pseudocode:
1) Define the base case:
* If `cake` is `None`, return `0`.
2) Recursively calculate the depth of the left subtree (`left_depth = max_tiers(cake.left)`).
3) Recursively calculate the depth of the right subtree (`right_depth = max_tiers(cake.right)`).
4) Return `1 + max(left_depth, right_depth)` as the maximum number of tiers.
4: I-mplement
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 max_tiers(cake):
if not cake:
return 0
left_depth = max_tiers(cake.left)
right_depth = max_tiers(cake.right)
return 1 + max(left_depth, right_depth)
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with the input
cake_sections = ["Chocolate", "Vanilla", "Strawberry", None, None, "Chocolate", "Coffee"]
:- The longest path should be correctly identified as having a maximum of 3 tiers.
6: E-valuate
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.
- Time Complexity:
O(N)
because each node in the tree must be visited once. - Space Complexity:
O(N)
due to the recursive call stack in the worst case, assuming a balanced tree.