Unit 8 Session 1 Standard (Click for link to problem statements)
Unit 8 Session 1 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: Binary tree where the rightmost path is ["Root", "Node2", "Leaf3"]
Output: ["Root", "Node2", "Leaf3"]
Explanation: The rightmost path is extracted correctly using recursion.
EDGE CASE
Input: Binary tree with no right child at any node
Output: ["Root"]
Explanation: The rightmost path consists only of the root 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 Traversal problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree recursively starting from the root and collect the values of nodes in the rightmost path.
1) If the current node is None, return an empty list.
2) Return a list containing the current node’s value, followed by the result of recursively calling the function on the right child.
⚠️ Common Mistakes
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 right_vine(root):
if root is None:
return []
return [root.val] + right_vine(root.right)
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.
Assume H
represents the height of the binary tree.
O(H)
because the algorithm traverses down the height of the tree.O(H)
because the recursion stack will use space proportional to the height of the tree.