Lowest Common Ancestor of a Binary Search Tree LeetCode solution in C++

1
class Solution {
2
public:
3
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) {
4
if (p->val < root->val && q->val < root->val)
5
return lowestCommonAncestor(root->left, p, q);
6
else if (p->val > root->val && q->val > root->val)
7
return lowestCommonAncestor(root->right, p, q);
8
return root;
9
}
10
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0