Unit 8 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?
HAPPY CASE
Input: TreeNode(10, TreeNode(5), TreeNode(15, None, TreeNode(20)))
Output: [20, 5]
Explanation: The leaves are the nodes with values 5 and 20, and they are listed in descending order.
EDGE CASE
Input: TreeNode(10)
Output: [10]
Explanation: The single node is both a root and a leaf.
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.
This is a leaf collection problem that involves tree traversal and sorting. The strategy aligns with post-order traversal to ensure all leaves are accessed before sorting.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree to collect leaf nodes, then sort the collected values in descending order.
1) Define a helper function to traverse the tree and collect leaf values.
2) Start from the root and traverse to the right subtree first to facilitate descending order collection.
3) Check each node to determine if it is a leaf (i.e., no left or right children).
4) Store leaf values.
5) After traversal, the leaves collected from right to left are already in descending order.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def collect_leaves_descending(root, leaves):
"
Helper function to collect leaf nodes' values from the BST rooted at `root` in descending order.
The values are collected directly into the list `leaves`.
"
if root is None:
return
# First, visit the right child (to ensure descending order)
collect_leaves_descending(root.right, leaves)
# Check if it's a leaf node
if root.left is None and root.right is None:
leaves.append(root.val)
# Then, visit the left child
collect_leaves_descending(root.left, leaves)
def descending_leaves(root):
"
Return a list of leaf values in descending order from the BST rooted at `root`.
"
leaves = []
collect_leaves_descending(root, leaves)
return leaves
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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. Each node is visited once.O(h)
where h is the height of the tree due to recursion, which can be up to O(n)
in unbalanced trees but is typically O(log n)
in balanced trees.