Codepath

Find the Lowest Common Ancestor in a Plant Tree Based on Species Names

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

Problem Highlights

  • 💡 Difficulty: Medium-Hard
  • Time to complete: 30-35 mins
  • 🛠️ Topics: Trees, Lowest Common Ancestor, 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 is the structure of the tree?
    • The tree is a binary tree where each node represents a different plant species.
  • What operation needs to be performed?
    • The function needs to find the lowest common ancestor (LCA) of two given species in the tree.
  • What should be returned?
    • The function should return the species name of the lowest common ancestor.
HAPPY CASE
Input: 
    fern = TreeNode("fern")
    cactus = TreeNode("cactus", fern)
    rose = TreeNode("rose", fern)
    bamboo = TreeNode("bamboo", cactus)
    dahlia = TreeNode("dahlia", cactus)
    lily = TreeNode("lily", rose)
    oak = TreeNode("oak", rose)

    fern.left, fern.right = cactus, rose
    cactus.left, cactus.right = bamboo, dahlia
    rose.left, rose.right = lily, oak

    lca(fern, "cactus", "rose")
Output: 
    "fern"
Explanation: 
    The lowest common ancestor of "cactus" and "rose" is "fern" because "fern" is the lowest node in the tree that has both "cactus" and "rose" as descendants.

EDGE CASE
Input: 
    lca(fern, "bamboo", "oak")
Output: 
    "fern"
Explanation: 
    The lowest common ancestor of "bamboo" and "oak" is "fern" because "fern" is the lowest node in the tree that has both "bamboo" and "oak" as descendants.

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

  • Path to Root: Finding the path from each node to the root and then comparing these paths to find the lowest common ancestor.
  • Recursion: A recursive approach is natural here, as the LCA can be determined by examining the subtrees.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Find the path from each of the given species to the root, and then compare these paths to determine the lowest common ancestor.

1) Define a helper function `find_path_to_root(node)` that returns the path from the given node to the root.
    - Initialize an empty list `path`.
    - Traverse up the tree from the given node to the root, appending each node's species name to `path`.
    - Return `path`.
  
2) Define a helper function `find_node(root, species)` that returns the node with the given species name.
    - If the current node is `None`, return `None`.
    - If the current node's value matches `species`, return the current node.
    - Recursively search the left and right subtrees.
  
3) In the main `lca` function:
    - Use `find_node` to locate nodes `p` and `q`.
    - Use `find_path_to_root` to get the paths `path_p` and `path_q` from these nodes to the root.
    - Compare the paths from the root downwards until the paths diverge, keeping track of the last common node.
  
4) Return the species name of the last common node found in the comparison.

⚠️ Common Mistakes

  • Not correctly finding the path from the node to the root.
  • Incorrectly handling cases where one of the nodes is the root or is directly above the other.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode():
    def __init__(self, species, parent=None, left=None, right=None):
        self.val = species
        self.parent = parent
        self.left = left
        self.right = right

def lca(root, p, q):
    # Function to find the path from root to the given species
    def find_path_to_root(node):
        path = []
        while node:
            path.append(node.val)
            node = node.parent
        return path
    
    # Function to find a node with the given species name
    def find_node(root, species):
        if not root:
            return None
        if root.val == species:
            return root
        left_search = find_node(root.left, species)
        if left_search:
            return left_search
        return find_node(root.right, species)
    
    # Find paths from p and q to root
    node_p = find_node(root, p)
    node_q = find_node(root, q)
    
    path_p = find_path_to_root(node_p)
    path_q = find_path_to_root(node_q)
    
    # Find the lowest common ancestor by comparing the paths
    lca_species = None
    while path_p and path_q and path_p[-1] == path_q[-1]:
        lca_species = path_p.pop()
        path_q.pop()
    
    return lca_species

5: R-eview

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

- Example 1:
    - Input: 
        `fern = TreeNode("fern")`
        `cactus = TreeNode("cactus", fern)`
        `rose = TreeNode("rose", fern)`
        `bamboo = TreeNode("bamboo", cactus)`
        `dahlia = TreeNode("dahlia", cactus)`
        `lily = TreeNode("lily", rose)`
        `oak = TreeNode("oak", rose)`
        
        `fern.left, fern.right = cactus, rose`
        `cactus.left, cactus.right = bamboo, dahlia`
        `rose.left, rose.right = lily, oak`
        `lca(fern, "cactus", "rose")`
    - Execution: 
        - Find paths: cactus -> fern, rose -> fern.
        - Compare paths: LCA is fern.
    - Output: 
        "fern"
- Example 2:
    - Input: 
        `lca(fern, "bamboo", "oak")`
    - Execution: 
        - Find paths: bamboo -> cactus -> fern, oak -> rose -> fern.
        - Compare paths: LCA is fern.
    - Output: 
        "fern"

6: E-valuate

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

Time Complexity:

  • Time Complexity: O(N) where N is the number of nodes in the tree.
    • Explanation: We may need to visit each node to find the path to the root for both species.

Space Complexity:

  • Space Complexity: O(H) where H is the height of the tree.
    • Explanation: The recursion stack and the paths to the root both use space proportional to the height of the tree, which is O(log N) in a balanced tree.
Fork me on GitHub