0
0
DSA C++programming~20 mins

Lowest Common Ancestor in Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LCA Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LCA function for given binary tree and nodes
What is the output of the following C++ code that finds the Lowest Common Ancestor (LCA) of two nodes in a binary tree?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* findLCA(Node* root, int n1, int n2) {
    if (!root) return nullptr;
    if (root->val == n1 || root->val == n2) return root;
    Node* left_lca = findLCA(root->left, n1, n2);
    Node* right_lca = findLCA(root->right, n1, n2);
    if (left_lca && right_lca) return root;
    return (left_lca != nullptr) ? left_lca : right_lca;
}

int main() {
    Node* root = new Node(3);
    root->left = new Node(5);
    root->right = new Node(1);
    root->left->left = new Node(6);
    root->left->right = new Node(2);
    root->right->left = new Node(0);
    root->right->right = new Node(8);
    root->left->right->left = new Node(7);
    root->left->right->right = new Node(4);

    Node* lca = findLCA(root, 7, 4);
    if (lca) std::cout << lca->val << std::endl;
    else std::cout << "No LCA found" << std::endl;
    return 0;
}
A3
B5
C7
D2
Attempts:
2 left
💡 Hint
Think about where nodes 7 and 4 are located in the tree and which node is their closest shared ancestor.
🧠 Conceptual
intermediate
1:30remaining
Understanding the base case in LCA recursive function
In the recursive function to find the Lowest Common Ancestor (LCA) in a binary tree, what is the purpose of the base case that returns the root if root's value matches either of the target nodes?
AIt stops recursion when one of the target nodes is found, marking a potential ancestor.
BIt skips the current node and continues searching in children.
CIt immediately returns the root's parent node.
DIt returns null to indicate the node is not found here.
Attempts:
2 left
💡 Hint
Think about why the function needs to know when it has found one of the nodes.
🔧 Debug
advanced
2:00remaining
Identify the error in this LCA function variant
What error will the following C++ code produce when trying to find the LCA of nodes 5 and 1?
DSA C++
Node* findLCA(Node* root, int n1, int n2) {
    if (!root) return nullptr;
    if (root->val == n1 || root->val == n2) return root;
    Node* left_lca = findLCA(root->left, n1, n2);
    Node* right_lca = findLCA(root->right, n1, n2);
    if (left_lca && right_lca) return root;
    if (left_lca) return left_lca;
    if (right_lca) return right_lca;
    // Missing return statement here
    return nullptr;
}
ACompilation error due to missing return statement
BRuntime error: segmentation fault
CReturns nullptr without error
DReturns wrong node value
Attempts:
2 left
💡 Hint
Check if all code paths return a value.
Predict Output
advanced
2:00remaining
Output of LCA function when one node is not present
What is the output of the following code when searching for the LCA of nodes 5 and 10, where 10 does not exist in the tree?
DSA C++
struct Node {
    int val;
    Node* left;
    Node* right;
    Node(int x) : val(x), left(nullptr), right(nullptr) {}
};

Node* findLCA(Node* root, int n1, int n2) {
    if (!root) return nullptr;
    if (root->val == n1 || root->val == n2) return root;
    Node* left_lca = findLCA(root->left, n1, n2);
    Node* right_lca = findLCA(root->right, n1, n2);
    if (left_lca && right_lca) return root;
    return (left_lca != nullptr) ? left_lca : right_lca;
}

int main() {
    Node* root = new Node(3);
    root->left = new Node(5);
    root->right = new Node(1);
    root->left->left = new Node(6);
    root->left->right = new Node(2);
    root->right->left = new Node(0);
    root->right->right = new Node(8);
    root->left->right->left = new Node(7);
    root->left->right->right = new Node(4);

    Node* lca = findLCA(root, 5, 10);
    if (lca) std::cout << lca->val << std::endl;
    else std::cout << "No LCA found" << std::endl;
    return 0;
}
A3
BNo LCA found
C5
D10
Attempts:
2 left
💡 Hint
Consider what happens when one node is found but the other is missing.
🧠 Conceptual
expert
2:30remaining
Why does the LCA algorithm work without parent pointers?
Why can the Lowest Common Ancestor (LCA) be found in a binary tree using only recursive calls without storing parent pointers?
ABecause the tree nodes store references to their ancestors internally.
BBecause recursion explores all paths and returns nodes upward, allowing the function to detect where paths to both nodes diverge.
CBecause the algorithm uses a global variable to track parents during traversal.
DBecause the tree is always a binary search tree, so parent pointers are unnecessary.
Attempts:
2 left
💡 Hint
Think about how recursion returns information from child nodes to parent nodes.