We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4d75789 commit 3b7f9a9Copy full SHA for 3b7f9a9
lowest-common-ancestor-of-a-binary-search-tree/PDKhan.cpp
@@ -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