Skip to content

Commit d8935e5

Browse files
authored
Create main.cpp
1 parent 2cfe89c commit d8935e5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
class Solution {
12+
public:
13+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
14+
if(root == NULL) return NULL;
15+
16+
if(root -> val < p -> val && root -> val < q -> val) {
17+
return lowestCommonAncestor(root -> right, p, q);
18+
}
19+
20+
if(root -> val > p -> val && root -> val > q -> val){
21+
return lowestCommonAncestor(root -> left, p, q);
22+
}
23+
24+
return root;
25+
}
26+
};

0 commit comments

Comments
 (0)