Lowest Common Ancestor of a Binary Search Tree LeetCode solution in C++
1class Solution {2public:3TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {4if (p->val < root->val && q->val < root->val)5return lowestCommonAncestor(root->left, p, q);6else if (p->val > root->val && q->val > root->val)7return lowestCommonAncestor(root->right, p, q);8return root;9}10};