Skip to content

Biggest Pearl

Raymond Chen edited this page Sep 2, 2024 · 1 revision

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

Problem Highlights

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

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 each node in the binary tree represent?
    • Each node represents the size of a pearl in an oyster.
  • What does the function need to return?
    • The function needs to return the size of the largest pearl in the tree.
  • How should the function behave if the tree is empty?
    • The function should return a very small value (e.g., negative infinity) since no pearls exist.
HAPPY CASE
Input: Binary tree with nodes [7, 6, 0, 5, 1]
Output: 7
Explanation: The largest pearl has a size of 7.

EDGE CASE
Input: Binary tree with nodes [1, 0, 1]
Output: 1
Explanation: The largest pearl has a size of 1.

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

  • Binary Tree Traversal: Traverse the tree to find the largest node value.
  • Recursion: Use recursion to compare the node values in each subtree and return the maximum.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, comparing the values of each node to find the maximum.

1) If the current node is None, return a very small value (negative infinity).
2) Recursively find the maximum value in the left subtree.
3) Recursively find the maximum value in the right subtree.
4) Return the maximum of the current node's value, the maximum value in the left subtree, and the maximum value in the right subtree.

⚠️ Common Mistakes

  • Not correctly handling the base case where the tree is empty.
  • Forgetting to compare the current node's value when finding the maximum.

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 find_largest_pearl(root):
    if root is None:
        return float('-inf')
    
    # Recursively find the maximum value in the left and right subtrees
    left_max = find_largest_pearl(root.left)
    right_max = find_largest_pearl(root.right)
    
    # Return the maximum value found
    return max(root.val, left_max, right_max)

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 with nodes [7, 6, 0, 5, 1]
    - Expected Output: 7
    
    - Input 2: Binary tree with nodes [1, 0, 1]
    - Expected Output: 1
    

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 binary tree.

  • Time Complexity: O(N) because the algorithm needs to visit each node in the tree.
  • Space Complexity: O(H) where H is the height of the tree, due to the recursive call stack.
Clone this wiki locally