Challenge - 5 Problems
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LCA for given nodes in a binary tree
What is the output of the following code that finds the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def lca(root, p, q): if root is None or root.val == p or root.val == 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 # Construct the tree root = Node(3) root.left = Node(5) root.right = Node(1) root.left.left = Node(6) root.left.right = Node(2) root.right.left = Node(0) root.right.right = Node(8) root.left.right.left = Node(7) root.left.right.right = Node(4) ancestor = lca(root, 5, 1) print(ancestor.val if ancestor else None)
Attempts:
2 left
💡 Hint
Think about where the paths to nodes 5 and 1 meet in the tree.
✗ Incorrect
The LCA of nodes 5 and 1 is the root node 3 because 5 is in the left subtree and 1 is in the right subtree of 3.
❓ Predict Output
intermediateOutput of LCA for nodes in same subtree
What is the output of the code that finds the Lowest Common Ancestor (LCA) of nodes 7 and 4 in the binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def lca(root, p, q): if root is None or root.val == p or root.val == 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 # Construct the tree root = Node(3) root.left = Node(5) root.right = Node(1) root.left.left = Node(6) root.left.right = Node(2) root.right.left = Node(0) root.right.right = Node(8) root.left.right.left = Node(7) root.left.right.right = Node(4) ancestor = lca(root, 7, 4) print(ancestor.val if ancestor else None)
Attempts:
2 left
💡 Hint
Both nodes are in the right subtree of node 5.
✗ Incorrect
Nodes 7 and 4 are both children of node 2, so the LCA is node 2.
🔧 Debug
advancedIdentify the error in LCA function
What error will the following code raise when trying to find the LCA of nodes 5 and 6?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def lca(root, p, q): if root is None: return None if root.val == p: return root if root.val == 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 root = Node(3) root.left = Node(5) root.right = Node(1) ancestor = lca(root, 5, 6) print(ancestor.val)
Attempts:
2 left
💡 Hint
Check what the function returns when root.val matches p or q.
✗ Incorrect
The function returns the integer p or q instead of the node object, so accessing ancestor.val causes AttributeError.
🧠 Conceptual
advancedUnderstanding LCA when one node is ancestor of the other
In a binary tree, if one node is the ancestor of the other, what will the Lowest Common Ancestor (LCA) function return?
Attempts:
2 left
💡 Hint
Think about the definition of ancestor and how the LCA function works.
✗ Incorrect
If one node is ancestor of the other, the LCA is the ancestor node itself because it is the lowest node that contains both.
🚀 Application
expertFind LCA in a binary tree with missing nodes
Given the binary tree below, what is the output of finding the LCA of nodes 7 and 10?
Tree structure:
- 3
- 5
- 6
- 2
- 7
- 4
- 1
- 0
- 8
Note: Node 10 does not exist in the tree.
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def lca(root, p, q): if root is None or root.val == p or root.val == 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 root = Node(3) root.left = Node(5) root.right = Node(1) root.left.left = Node(6) root.left.right = Node(2) root.right.left = Node(0) root.right.right = Node(8) root.left.right.left = Node(7) root.left.right.right = Node(4) ancestor = lca(root, 7, 10) print(ancestor.val if ancestor else None)
Attempts:
2 left
💡 Hint
If one node is missing, the function returns the node found if any.
✗ Incorrect
Since node 10 is missing, the function returns node 7 because it found node 7 but not 10.
