Unit 9 Session 1 (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:
family1 = TreeNode("Isadora the Hexed",
TreeNode("Thorne",
TreeNode("Dracula"),
TreeNode("Doom", TreeNode("Gloom"), TreeNode("Mortis"))),
TreeNode("Raven",
TreeNode("Hecate"),
TreeNode("Wraith")))
Output:
"Doom"
Explanation:
* Gloom and Mortis are the deepest leaves. Their lowest common ancestor is Doom.
Input:
family2 = TreeNode("Grandmama Addams",
TreeNode("Gomez Addams", None, TreeNode("Wednesday Addams")),
TreeNode("Uncle Fester"))
Output:
"Wednesday Addams"
Explanation:
* The only deepest leaf is Wednesday Addams, so it is its own lowest common ancestor.
EDGE CASE
Input: family = TreeNode("Alone")
Output: "Alone"
Explanation: The tree has only one node, so return that node's value as it is the LCA of itself.
Input: family = None
Output: None
Explanation: The tree is empty, so return `None`.
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 problems involving finding the lowest common ancestor (LCA) of nodes in a binary tree, we can consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
1) Identify Deepest Leaves:
2) Find LCA of Deepest Leaves:
3) Return the LCA Value:
Pseudocode:
1) If `root` is `None`, return `None`.
2) Initialize a list `leaves` to store the deepest leaf nodes.
3) Define a recursive function `find_deepest_leaves(node, depth)`:
a) If `node` is `None`, return.
b) If `node` is a leaf, compare its depth with `max_depth`.
i) If `depth` > `max_depth`, update `max_depth` and reset `leaves` with this node.
ii) If `depth` == `max_depth`, append the node to `leaves`.
c) Recur for left and right children.
4) Define a recursive function `lca(root, p, q)` to find the LCA of two nodes:
a) If `root` is `None` or equals `p` or `q`, return `root`.
b) Recur on the left and right children.
c) If both left and right subtrees return non-`None`, return `root`.
d) Otherwise, return the non-`None` subtree.
5) Call `find_deepest_leaves` starting from the root node.
6) Iterate through `leaves` and find their LCA using the `lca` function.
7) Return the value of the LCA node.
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, key, value, left=None, right=None):
self.key = key
self.val = value
self.left = left
self.right = right
def find_deepest_leaves(root):
max_depth = -1
leaves = []
def dfs(node, depth):
nonlocal max_depth, leaves
if not node:
return
# If this is a leaf node
if not node.left and not node.right:
if depth > max_depth:
max_depth = depth
leaves = [node]
elif depth == max_depth:
leaves.append(node)
else:
dfs(node.left, depth + 1)
dfs(node.right, depth + 1)
dfs(root, 0)
return leaves
def lca(root, p, q):
if not root or root == p or root == q:
return root
left = lca(root.left, p, q)
right = lca(root.right, p, q)
if left and right:
return root
return left if left else right
def lca_youngest_children(root):
# Step 1: Find the deepest leaves
deepest_leaves = find_deepest_leaves(root)
# Step 2: Find their LCA
lca_node = deepest_leaves[0]
for leaf in deepest_leaves[1:]:
lca_node = lca(root, lca_node, leaf)
return lca_node.val
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
family1 = TreeNode("Isadora the Hexed", TreeNode("Thorne", TreeNode("Dracula"), TreeNode("Doom", TreeNode("Gloom"), TreeNode("Mortis"))), TreeNode("Raven", TreeNode("Hecate"), TreeNode("Wraith")))
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(N)
because each node in the tree must be visited during the DFS traversal and the LCA computation.O(N)
due to the recursive call stack, which can go as deep as the height of the tree.