Skip to content

Commit 1465679

Browse files
committedSep 10, 2021
feat: add cpp solution to lc problem: No.0235.Lowest Common Ancestor of a Binary Search Tree
1 parent a5e177d commit 1465679

File tree

6 files changed

+140
-8
lines changed

6 files changed

+140
-8
lines changed
 

‎basic/sorting/BubbleSort/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ int main(void)
162162
}
163163
```
164164
165+
<!-- tabs:end -->
166+
165167
## 算法分析
166168
167169
空间复杂度 O(1)、时间复杂度 O(n²)。

‎basic/summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
- [选择排序](/basic/sorting/SelectionSort/README.md)
66
- [归并排序](/basic/sorting/MergeSort/README.md)
77
- [快速排序](/basic/sorting/QuickSort/README.md)
8-
- [堆排序](/basic/sortring/HeapSort/README.md)
8+
- [堆排序](/basic/sorting/HeapSort/README.md)
99
- 查找算法
1010
- [二分查找](/basic/searching/BinarySearch/README.md)

‎index.html

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>LeetCode & Coding Interview Guide</title>
77
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
88
<meta name="keywords"
9-
content="doc,docs,doocs,documentation,github,coding,pages,leetcode,coding-interview,coding-interview-guide,cracking-the-coding-interview,yanglbme">
9+
content="doc,docs,doocs,documentation,github,gitee,coding,pages,leetcode,coding-interview,coding-interview-guide,cracking-the-coding-interview,yanglbme">
1010
<meta name="description" content="LeetCode、剑指Offer、程序员面试金典题解">
1111
<meta name="viewport"
1212
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
@@ -80,14 +80,16 @@
8080
.replace(/\/main/, '/blob/main') : 'https://github.com/doocs/leetcode/blob/main/' + vm.route.file
8181

8282
const github = `[GitHub](${url})`
83-
const editHtml = en ? `:memo: Edit on ${github}\n` : `:memo: 在 ${github} 编辑\n`
83+
const gitee = `[Gitee](${url.replace("github", "gitee")})`
84+
const editHtml = en ? `:memo: Edit on ${github} / ${gitee}\n` : `:memo: 在 ${github} / ${gitee} 编辑\n`
8485
return editHtml + html
8586
})
8687

8788
hook.afterEach(function (html) {
8889
const en = vm.route.file.indexOf('README_EN') != -1
8990
const copyright = en ? '. All Rights Reserved' : ' 版权所有'
90-
const footer = `<footer><span>Copyright © 2018-2021 <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`
91+
const currentYear = new Date().getFullYear()
92+
const footer = `<footer><span>Copyright © 2018-${currentYear} <a href="https://github.com/doocs" target="_blank">Doocs</a>${copyright}</footer>`
9193
return html + footer
9294
})
9395
},
@@ -104,8 +106,8 @@
104106
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-java.min.js"></script>
105107
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-kotlin.min.js"></script>
106108
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-csharp.min.js"></script>
107-
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-javascript.min.js"></script>
108-
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-typescript.min.js"></script>
109+
<script src="//cdn.jsdelivr.net/npm/prismjs@1.15.0/components/prism-javascript.min.js"></script>
110+
<script src="//cdn.jsdelivr.net/npm/prismjs@1.15.0/components/prism-typescript.min.js"></script>
109111
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-python.min.js"></script>
110112
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-swift.min.js"></script>
111113
<script src="//cdn.jsdelivr.net/npm/prismjs/components/prism-nim.min.js"></script>

‎solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,57 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
258258
}
259259
```
260260

261+
### **C++**
262+
263+
迭代:
264+
265+
```cpp
266+
/**
267+
* Definition for a binary tree node.
268+
* struct TreeNode {
269+
* int val;
270+
* TreeNode *left;
271+
* TreeNode *right;
272+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
273+
* };
274+
*/
275+
276+
class Solution {
277+
public:
278+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
279+
while (root)
280+
{
281+
if (root->val < p->val && root->val < q->val) root = root->right;
282+
else if (root->val > p->val && root->val > q->val) root = root->left;
283+
else return root;
284+
}
285+
return root;
286+
}
287+
};
288+
```
289+
290+
递归:
291+
292+
```cpp
293+
/**
294+
* Definition for a binary tree node.
295+
* struct TreeNode {
296+
* int val;
297+
* TreeNode *left;
298+
* TreeNode *right;
299+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
300+
* };
301+
*/
302+
303+
class Solution {
304+
public:
305+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
306+
if (!root) return root;
307+
if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
308+
if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
309+
return root;
310+
}
311+
};
312+
```
313+
261314
<!-- tabs:end -->

‎solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/README_EN.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class Solution {
144144

145145
### **TypeScript**
146146

147-
迭代:
147+
Iterative:
148148

149149
```ts
150150
/**
@@ -174,7 +174,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
174174
};
175175
```
176176

177-
递归:
177+
Recursive:
178178

179179
```ts
180180
/**
@@ -252,6 +252,59 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
252252
}
253253
```
254254

255+
### **C++**
256+
257+
Iterative:
258+
259+
```cpp
260+
/**
261+
* Definition for a binary tree node.
262+
* struct TreeNode {
263+
* int val;
264+
* TreeNode *left;
265+
* TreeNode *right;
266+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
267+
* };
268+
*/
269+
270+
class Solution {
271+
public:
272+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
273+
while (root)
274+
{
275+
if (root->val < p->val && root->val < q->val) root = root->right;
276+
else if (root->val > p->val && root->val > q->val) root = root->left;
277+
else return root;
278+
}
279+
return root;
280+
}
281+
};
282+
```
283+
284+
Recursive:
285+
286+
```cpp
287+
/**
288+
* Definition for a binary tree node.
289+
* struct TreeNode {
290+
* int val;
291+
* TreeNode *left;
292+
* TreeNode *right;
293+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
294+
* };
295+
*/
296+
297+
class Solution {
298+
public:
299+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
300+
if (!root) return root;
301+
if (root->val < p->val && root->val < q->val) return lowestCommonAncestor(root->right, p, q);
302+
if (root->val > p->val && root->val > q->val) return lowestCommonAncestor(root->left, p, q);
303+
return root;
304+
}
305+
};
306+
```
307+
255308
### **...**
256309

257310
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
while (root)
15+
{
16+
if (root->val < p->val && root->val < q->val) root = root->right;
17+
else if (root->val > p->val && root->val > q->val) root = root->left;
18+
else return root;
19+
}
20+
return root;
21+
}
22+
};

0 commit comments

Comments
 (0)