📖 Scenario: You are working on a family tree application. You want to find the closest common ancestor of two family members in the tree.
🎯 Goal: Build a program that finds the Lowest Common Ancestor (LCA) of two nodes in a binary tree.
📋 What You'll Learn
Create a binary tree with specific nodes
Add two variables to hold the target nodes
Write a function to find the LCA of the two nodes
Print the value of the LCA node
💡 Why This Matters
🌍 Real World
Finding common ancestors is useful in family trees, organizational charts, and version control systems to find shared history.
💼 Career
Understanding tree traversal and recursion is important for software engineers working with hierarchical data and algorithms.
Progress0 / 4 steps
1
Create the Binary Tree
Create a class called TreeNode with an int val, and two TreeNode references called left and right. Then create a binary tree with root node value 3, left child 5, right child 1, left child of 5 as 6, right child of 5 as 2, left child of 1 as 0, and right child of 1 as 8. Assign this tree to a variable called root.
DSA C#
Hint
Define the TreeNode class first, then build the tree from bottom up using the constructor.
2
Set the Target Nodes
Create two variables called p and q and assign them to the nodes with values 5 and 1 in the tree respectively.
DSA C#
Hint
Use root.left to get the node with value 5 and root.right to get the node with value 1.
3
Write the Lowest Common Ancestor Function
Write a function called LowestCommonAncestor that takes TreeNode root, TreeNode p, and TreeNode q as parameters and returns the lowest common ancestor node. Use recursion to check if root is null, or equal to p or q. Recursively call the function on root.left and root.right. If both calls return non-null, return root. Otherwise, return the non-null result.
DSA C#
Hint
Use recursion to search left and right subtrees. Return root if it matches p or q. If both sides return nodes, root is the LCA.
4
Print the Lowest Common Ancestor Value
Call the LowestCommonAncestor function with root, p, and q and print the val of the returned node.
DSA C#
Hint
Call LowestCommonAncestor(root, p, q) and print the val of the result using Console.WriteLine.
Practice
(1/5)
1. What is the Lowest Common Ancestor (LCA) of two nodes in a binary tree?
easy
A. The node that is a child of both nodes
B. The root node of the binary tree
C. The node with the smallest value between the two nodes
D. The deepest node that is an ancestor of both nodes
Solution
Step 1: Understand the definition of LCA
The Lowest Common Ancestor is the deepest node in the tree that has both given nodes as descendants (a node can be a descendant of itself).
Step 2: Identify the correct description
Among the options, only the deepest common ancestor fits this definition.
Final Answer:
The deepest node that is an ancestor of both nodes -> Option D
Quick Check:
LCA = deepest common ancestor [OK]
Hint: LCA is the lowest node common to both paths [OK]
Common Mistakes:
Confusing LCA with root node
Thinking LCA is the smaller value node
Assuming LCA is a child of both nodes
2. Which of the following is the correct C# method signature to find the LCA in a binary tree node class named TreeNode?
easy
A. public TreeNode FindLCA(TreeNode root, TreeNode p, TreeNode q)
B. public void FindLCA(TreeNode root, int p, int q)
C. private int FindLCA(int root, int p, int q)
D. public TreeNode FindLCA(int root, TreeNode p, TreeNode q)
Solution
Step 1: Analyze method parameters and return type
The method should accept the root node and two nodes to find LCA for, and return a TreeNode representing the LCA.
Step 2: Match signature with correct types
public TreeNode FindLCA(TreeNode root, TreeNode p, TreeNode q) correctly uses TreeNode for all nodes and returns TreeNode. Other options have incorrect parameter types or return types.
Final Answer:
public TreeNode FindLCA(TreeNode root, TreeNode p, TreeNode q) -> Option A
Quick Check:
Correct signature uses TreeNode parameters and return type [OK]
Hint: LCA method returns TreeNode and takes TreeNode parameters [OK]
Common Mistakes:
Using int instead of TreeNode for nodes
Wrong return type (void or int)
Incorrect access modifier or parameter order
3. Given the following binary tree and code snippet, what will be the output of FindLCA(root, node5, node1).val?
Node 5 is left child of root 3, node 1 is right child of root 3.
Step 2: Find lowest common ancestor
The root node 3 is the lowest node that has both 5 and 1 as descendants.
Final Answer:
3 -> Option B
Quick Check:
LCA(5,1) = 3 [OK]
Hint: LCA of nodes on different sides is their root ancestor [OK]
Common Mistakes:
Choosing one of the nodes instead of their ancestor
Confusing node values with LCA
Ignoring tree structure
4. Identify the bug in this C# method to find LCA in a binary tree:
public TreeNode FindLCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (root == p || root == q) return root;
TreeNode left = FindLCA(root.left, p, q);
TreeNode right = FindLCA(root.right, p, q);
if (left != null && right != null) return root;
return left != null ? left : right;
}
medium
A. Missing base case for leaf nodes
B. Should check if root.val equals p.val or q.val instead of node references
C. No bug, method is correct
D. Return statement should always return root
Solution
Step 1: Review base cases and recursive calls
The method returns null if root is null, returns root if root matches p or q, then recursively searches left and right subtrees.
Step 2: Check logic for combining results
If both left and right are non-null, root is LCA; else returns whichever subtree found a node. This is correct logic.
Final Answer:
No bug, method is correct -> Option C
Quick Check:
Standard LCA recursive method is correct [OK]
Hint: Check if both sides return non-null to find LCA [OK]
Common Mistakes:
Comparing node values instead of references if nodes are unique
Missing base case for null root
Incorrect return logic
5. You have a binary tree where nodes may not be unique. How would you modify the LCA method to find the LCA of two nodes given only their values (int pVal, int qVal), assuming multiple nodes can have the same value?
hard
A. Use a method that finds paths from root to each node value and then compare paths to find LCA
B. Modify the method to return the first node found with matching value and use parent pointers to find LCA
C. Return the root node always since values are not unique
D. Use a hash set to store visited nodes and return the first common node visited
Solution
Step 1: Understand the problem with non-unique values
Since values are not unique, we cannot rely on node references or values alone to find LCA directly.
Step 2: Use path comparison approach
Find paths from root to each node value (may find multiple nodes), then compare paths to find the last common node, which is the LCA.
Final Answer:
Use a method that finds paths from root to each node value and then compare paths to find LCA -> Option A
Quick Check:
Path comparison works for non-unique values [OK]
Hint: Find paths for both values, then compare to find LCA [OK]
Common Mistakes:
Assuming node references are unique
Returning root always
Using hash set without paths
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.