Skip to content

Commit 82c7a2c

Browse files
third week
1 parent 7e8e3c3 commit 82c7a2c

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

Week_03/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1-
学习笔记
1+
# 作业
2+
3+
+ [二叉树的最近公共祖先](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
4+
5+
```c++
6+
class Solution {
7+
public:
8+
TreeNode* ans;
9+
bool dfs(TreeNode* root, TreeNode* p, TreeNode* q) {
10+
if (root == nullptr) return false;
11+
bool lson = dfs(root->left, p, q);
12+
bool rson = dfs(root->right, p, q);
13+
if ((lson && rson) || ((root->val == p->val || root->val == q->val) && (lson || rson))) {
14+
ans = root;
15+
}
16+
return lson || rson || (root->val == p->val || root->val == q->val);
17+
}
18+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
19+
dfs(root, p, q);
20+
return ans;
21+
}
22+
};
23+
```
24+
25+
+ [组合](https://leetcode-cn.com/problems/combinations/)
26+
27+
```c++
28+
class Solution:
29+
def combine(self, n: int, k: int) -> List[List[int]]:
30+
if n < k or n < 1:
31+
return []
32+
if k == 0:
33+
return [[]]
34+
if n == k:
35+
return [[i for i in range(1, n+1)]]
36+
ans1 = self.combine(n-1, k-1)
37+
ans2 = self.combine(n-1, k)
38+
print(n, k, ans1, ans2)
39+
if ans1:
40+
for i in ans1:
41+
i.append(n)
42+
return ans1+ans2
43+
44+
```
45+
46+
# 总结
47+
48+
+ 本结知识确实比较薄弱,还需要多刷题,多理解。
49+
+ 记住模板之后再刷题效果确实会好很多。

0 commit comments

Comments
 (0)