Challenge - 5 Problems
LCA Mastery
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 code when finding the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the given binary tree?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) return root; return left != null ? left : right; } public static void main(String[] args) { TreeNode root = new TreeNode(3); root.left = new TreeNode(5); root.right = new TreeNode(1); root.left.left = new TreeNode(6); root.left.right = new TreeNode(2); root.right.left = new TreeNode(0); root.right.right = new TreeNode(8); Solution sol = new Solution(); TreeNode lca = sol.lowestCommonAncestor(root, root.left, root.right); System.out.println(lca.val); } }
Attempts:
2 left
💡 Hint
Think about where the two 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 when one node is ancestor of the other
What is the output of the code when finding the Lowest Common Ancestor (LCA) of nodes 5 and 2 in the given binary tree?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) return root; return left != null ? left : right; } public static void main(String[] args) { TreeNode root = new TreeNode(3); root.left = new TreeNode(5); root.right = new TreeNode(1); root.left.left = new TreeNode(6); root.left.right = new TreeNode(2); root.right.left = new TreeNode(0); root.right.right = new TreeNode(8); Solution sol = new Solution(); TreeNode lca = sol.lowestCommonAncestor(root, root.left, root.left.right); System.out.println(lca.val); } }
Attempts:
2 left
💡 Hint
If one node is ancestor of the other, the ancestor is the LCA.
✗ Incorrect
Node 5 is an ancestor of node 2, so the LCA is node 5 itself.
🔧 Debug
advancedIdentify the error in LCA implementation
What error will this code produce when trying to find the LCA of nodes 5 and 1?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (root == p) return root; if (root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) return root; return left != null ? left : right; } public static void main(String[] args) { TreeNode root = new TreeNode(3); root.left = new TreeNode(5); root.right = new TreeNode(1); root.left.left = new TreeNode(6); root.left.right = new TreeNode(2); root.right.left = new TreeNode(0); root.right.right = new TreeNode(8); Solution sol = new Solution(); TreeNode lca = sol.lowestCommonAncestor(root, new TreeNode(5), new TreeNode(1)); System.out.println(lca.val); } }
Attempts:
2 left
💡 Hint
Check how nodes p and q are compared with root.
✗ Incorrect
The code compares nodes by reference but p and q are new nodes not linked to tree, so recursive calls return null and cause NullPointerException when accessing val.
🧠 Conceptual
advancedUnderstanding LCA when nodes are not in the tree
If one or both nodes are not present in the binary tree, what will the LCA function return?
Attempts:
2 left
💡 Hint
Think about what happens if recursive calls never find the nodes.
✗ Incorrect
If either node is missing, the recursive calls return null and the function returns null indicating no common ancestor found.
🚀 Application
expertFind LCA in a binary tree with parent pointers
Given a binary tree where each node has a pointer to its parent, which approach is the most efficient to find the LCA of two nodes p and q?
Attempts:
2 left
💡 Hint
Parent pointers allow moving up the tree easily.
✗ Incorrect
With parent pointers, we can move up from both nodes and track visited ancestors to find the first common one efficiently without starting from root.
