Skip to content

Commit 3b7f9a9

Browse files
committed
Lowest Common Ancestor solution
1 parent 4d75789 commit 3b7f9a9

File tree

1 file changed

+11
-0
lines changed
  • lowest-common-ancestor-of-a-binary-search-tree

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
4+
if(root->val < p->val && root->val < q->val)
5+
return lowestCommonAncestor(root->right, p, q);
6+
else if(root->val > p->val && root->val > q->val)
7+
return lowestCommonAncestor(root->left, p, q);
8+
else
9+
return root;
10+
}
11+
};

0 commit comments

Comments
 (0)