Count Old Growth Trees - codepath/compsci_guides GitHub Wiki

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 each node in the binary tree represent?
    • Each node represents the age of a tree in a forest.
  • What is considered an "old growth" tree?
    • A tree is considered "old growth" if its age (node value) is greater than a given threshold.
  • How should the function behave if the tree is empty?
    • The function should return 0 if the tree is empty.
HAPPY CASE
Input: Binary tree with nodes [100, 1200, 1500, 20, 700, 2600], threshold = 1000
Output: 3
Explanation: The nodes with values 1200, 1500, and 2600 are considered old growth trees.

EDGE CASE
Input: Binary tree with only one node [500], threshold = 1000
Output: 0
Explanation: The only node does not meet the threshold, so the count is 0.

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

  • Binary Tree Traversal: Traverse the tree to count nodes that meet the old growth criteria.
  • Recursion: Use recursion to count the number of nodes whose values exceed the threshold.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, counting nodes with values greater than the given threshold.

1) If the current node is None, return 0.
2) Recursively count old growth trees in the left subtree.
3) Recursively count old growth trees in the right subtree.
4) If the current node's value is greater than the threshold, add 1 to the total count.
5) Return the total count of old growth trees.

⚠️ Common Mistakes

  • Not correctly handling the base case where the tree is empty.
  • Forgetting to add the current node's count if it meets the threshold.

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 count_old_growth(root, threshold):
    if root is None:
        return 0
    
    # Recursively count old growth trees in the left and right subtrees
    left_count = count_old_growth(root.left, threshold)
    right_count = count_old_growth(root.right, threshold)
    
    # Check if the current node is an old growth tree
    if root.val > threshold:
        return 1 + left_count + right_count
    else:
        return left_count + right_count

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 [100, 1200, 1500, 20, 700, 2600], threshold = 1000
    • Expected Output: 3
    • Input 2: Binary tree with only one node [500], threshold = 1000
    • Expected Output: 0
    • Verify that the outputs match the expected results.

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 every node in the tree.
  • Space Complexity: O(H) where H is the height of the tree, due to the recursive call stack.