Skip to content

Shared Manager

Andrew Burke edited this page Aug 19, 2026 · 1 revision

TIP103 Unit 11 Session 2 (Click for link to problem statements)

Shared Manager

An org chart is stored as a binary search tree keyed by employee id. Given the root and two employee nodes p and q, find their lowest common ancestor: the deepest employee who manages both.

Return the value of that lowest common ancestor.

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

def lowest_common_ancestor(root, p, q):
    pass

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 20-25 mins
  • 🛠️ Topics: Binary Search Trees, Lowest Common Ancestor, Tree Traversal

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?
  • Q: What property of the org chart can we take advantage of?

    • A: It is a binary search tree keyed by employee id, so every id in a node's left subtree is smaller than the node's id, and every id in its right subtree is larger.
  • Q: Can an employee be their own manager for the purposes of this problem?

    • A: Yes. The lowest common ancestor is the deepest node that has both p and q as descendants, and a node counts as a descendant of itself. So if p manages q, then p is the lowest common ancestor.
  • Q: Should the function return the employee id or the node?

    • A: Return the lowest common ancestor node itself; the example usage accesses .val on the returned node to print the id.
HAPPY CASE
Input: root = the tree below, p = node 2, q = node 8
        6
       / \
      2   8
     / \ / \
    0  4 7  9
      / \
     3   5
Output: 6
Explanation: Employees 2 and 8 sit in different subtrees of the root, so the deepest employee who manages both is 6.

Input: root = the tree above, p = node 2, q = node 4
Output: 2
Explanation: Employee 2 directly manages employee 4, so 2 is the lowest common ancestor (an employee counts as their own ancestor).
EDGE CASE
Input: root = the tree above, p = node 3, q = node 5
Output: 4
Explanation: Both employees sit deep inside the same subtree; the search must keep descending past 6 and 2 to reach their shared manager 4.

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 Binary Search Tree Problems, we can consider the following approaches:

  • BST-Guided Traversal: Use the BST ordering property to decide at each node whether both targets lie left, both lie right, or split — the split point is the lowest common ancestor.
  • General LCA via Recursion: Works on any binary tree by searching both subtrees, but ignores the BST property and does more work than necessary.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Walk down from the root comparing the current node's id with p.val and q.val. If both ids are smaller, both employees are in the left subtree; if both are larger, both are in the right subtree. The first node where the two paths split — or where we land exactly on p or q — is the lowest common ancestor.

1) Set `current` to the root.
2) While `current` is not None:
   a) If both p.val and q.val are less than current.val, move `current` to current.left.
   b) Else if both p.val and q.val are greater than current.val, move `current` to current.right.
   c) Otherwise, the paths to p and q diverge here (or current is p or q itself), so return `current`.

⚠️ Common Mistakes

  • Ignoring the BST ordering and running a general-tree LCA search, which visits far more nodes than necessary.
  • Forgetting the case where current equals p or q: the loop must stop there, since an ancestor can be one of the two nodes itself.
  • Returning the employee id instead of the node; the caller expects a node and reads .val from it.

4: I-mplement

Implement the code to solve the algorithm.

def lowest_common_ancestor(root, p, q):
    current = root
    while current:
        if p.val < current.val and q.val < current.val:
            # Both employees have smaller ids, so both are managed within the left subtree
            current = current.left
        elif p.val > current.val and q.val > current.val:
            # Both employees have larger ids, so both are managed within the right subtree
            current = current.right
        else:
            # The paths to p and q split here (or current is p or q itself),
            # so this is the deepest employee managing both
            return current
    return None

5: R-eview

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

  • Input: root = the example org chart, p = node 2, q = node 8

    • current = 6: p.val (2) < 6 but q.val (8) > 6, so the paths split here.
    • Output: 6
  • Input: root = the example org chart, p = node 2, q = node 4

    • current = 6: both 2 and 4 are less than 6, move left.
    • current = 2: p.val (2) is not less than 2, so the split condition fires — current is p itself.
    • Output: 2
  • Input: root = the example org chart, p = node 3, q = node 5

    • current = 6: both 3 and 5 are less than 6, move left.
    • current = 2: both 3 and 5 are greater than 2, move right.
    • current = 4: 3 < 4 and 5 > 4, so the paths split here.
    • Output: 4

6: E-valuate

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

Assume N is the number of employees in the org chart and H is the height of the tree.

  • Time Complexity: O(H) because we follow a single path down from the root, moving one level deeper each iteration. For a balanced org chart this is O(log N); for a fully skewed one it degrades to O(N).
  • Space Complexity: O(1) because the traversal is iterative and uses only a single pointer, with no recursion stack or auxiliary storage.

Clone this wiki locally