0
0
DSA C++programming~10 mins

Lowest Common Ancestor in Binary Tree in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the current node is null.

DSA C++
if (root == [1]) return nullptr;
Drag options to blanks, or click blank then click option'
A0
BNULL
Cnullptr
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr
Checking for false instead of nullptr
2fill in blank
medium

Complete the code to check if the current node matches either of the target nodes.

DSA C++
if (root->val == [1] || root->val == q->val || root->val == p->val) return root;
Drag options to blanks, or click blank then click option'
Anullptr
Bp->val
Croot->val
Dq->val
Attempts:
3 left
💡 Hint
Common Mistakes
Using root->val instead of p->val
Using nullptr instead of a value
3fill in blank
hard

Fix the error in the recursive calls to search left and right subtrees.

DSA C++
TreeNode* left = [1](root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
Drag options to blanks, or click blank then click option'
AlowestCommonAncestor
BfindAncestor
ClowestCommon
DcommonAncestor
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined function names
Typos in function name
4fill in blank
hard

Fill both blanks to return the correct ancestor based on left and right subtree results.

DSA C++
if (left != [1] && right != [2]) return root;
Drag options to blanks, or click blank then click option'
Anullptr
BNULL
C0
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL or 0 instead of nullptr
Using false which is incorrect
5fill in blank
hard

Fill all three blanks to return the non-null subtree result or nullptr if none found.

DSA C++
return left != [1] ? left : (right != [2] ? right : [3]);
Drag options to blanks, or click blank then click option'
Anullptr
BNULL
Cfalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL or 0 instead of nullptr
Using false which is incorrect