Unit 8 Session 2 Advanced (Click for link to problem statements)
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?
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.
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:
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
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
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"
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of nodes in the tree.
O(H)
where H
is the height of the tree.
O(log N)
in a balanced tree.