Plant Classifications - 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 a plant classification category.
  • What does the function need to return?
    • The function needs to return an array of the most specific plant classification categories (the leaf node values).
  • How should the function behave if the tree is empty?
    • The function should return an empty list if the tree is empty.
HAPPY CASE
Input: Binary tree with nodes ["Plantae", "Non-flowering", "Flowering", "Mosses", "Ferns", "Gymnosperms", "Angiosperms", "Monocots", "Dicots"]
Output: ['Mosses', 'Ferns', 'Gymnosperms', 'Monocots', 'Dicots']
Explanation: The leaf nodes, which are the most specific categories, are ["Mosses", "Ferns", "Gymnosperms", "Monocots", "Dicots"].

EDGE CASE
Input: Binary tree with only one node ["Plantae"]
Output: ['Plantae']
Explanation: The tree has only the root, so the only classification is ["Plantae"].

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

  • Binary Tree Traversal: Traverse the tree to identify all the leaf nodes.
  • Recursion: Use recursion to collect the values of all leaf nodes.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, collecting the values of nodes that have no children (leaf nodes).

1) If the current node is None, return an empty list.
2) If the current node has no left and right children, it's a leaf node, so return a list containing its value.
3) Recursively collect leaf nodes from the left subtree.
4) Recursively collect leaf nodes from the right subtree.
5) Combine the lists of leaf nodes from the left and right subtrees and return the result.

⚠️ Common Mistakes

  • Not correctly handling the case where the tree has only the root node.
  • Forgetting to consider both left and right subtrees when collecting leaf nodes.

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 get_most_specific(taxonomy):
    if taxonomy is None:
        return []
    
    # If the current node is a leaf node, return its value
    if taxonomy.left is None and taxonomy.right is None:
        return [taxonomy.val]
    
    # Recursively collect leaf nodes from left and right subtrees
    left_leaves = get_most_specific(taxonomy.left)
    right_leaves = get_most_specific(taxonomy.right)
    
    # Combine the leaf nodes and return
    return left_leaves + right_leaves

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 ["Plantae", "Non-flowering", "Flowering", "Mosses", "Ferns", "Gymnosperms", "Angiosperms", "Monocots", "Dicots"]
    • Expected Output: ['Mosses', 'Ferns', 'Gymnosperms', 'Monocots', 'Dicots']
    • Input 2: Binary tree with only one node ["Plantae"]
    • Expected Output: ['Plantae']
    • 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.