Skip to content

Ivy Cutting

Raymond Chen edited this page Sep 2, 2024 · 2 revisions

Unit 8 Session 1 Standard (Click for link to problem statements)

Unit 8 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Binary Tree, Tree Traversal, Recursion

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 does the rightmost vine refer to?
    • The rightmost vine is the path from the root to the rightmost leaf node, following the right child at each step.
  • What should be returned if there is no right child?
    • The function should return a list containing only the root node value.
HAPPY CASE
Input: Binary tree where the rightmost path is ["Root", "Node2", "Leaf3"]
Output: ["Root", "Node2", "Leaf3"]
Explanation: The rightmost path is extracted correctly.

EDGE CASE
Input: Binary tree with no right child at any node
Output: ["Root"]
Explanation: The rightmost path consists only of the root node.

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 Tree Traversal problems, we want to consider the following approaches:

  • Binary Tree Traversal: Traverse the tree to extract the rightmost path.
  • Iteration: Use a loop to iterate down the rightmost nodes of the tree.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree starting from the root and collect the values of nodes in the rightmost path.

1) Initialize an empty list to store the result.
2) Set the current node to the root.
3) While the current node is not None:
    a) Add the current node's value to the result list.
    b) If the current node has a right child, move to the right child.
    c) If the current node does not have a right child, stop the loop.
4) Return the result list.

⚠️ Common Mistakes

  • Forgetting to handle cases where there are no right children, leading to an incomplete result.
  • Incorrectly assuming that every node has both left and right children.

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 right_vine(root):
    result = []
    current = root
    
    while current:
        result.append(current.val)
        if current.right:
            current = current.right
        else:
            break
            
    return result

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Test with the examples given:
    • Input 1: Binary tree where the rightmost path is ["Root", "Node2", "Leaf3"]
    • Expected Output: ["Root", "Node2", "Leaf3"]
    • Input 2: Binary tree with no right children
    • Expected Output: ["Root"]
    • Verify that the outputs are correct.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume H represents the height of the binary tree.

  • Time Complexity: O(H) because the algorithm traverses down the height of the tree.
  • Space Complexity: O(H) because we store the path in the result list.
Clone this wiki locally