Skip to content

Commit d65c487

Browse files
committed
1
1 parent b41b4d8 commit d65c487

File tree

7 files changed

+129
-1
lines changed

7 files changed

+129
-1
lines changed

notes/src/SUMMARY.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@
6464
- [day 18](./day18.md)
6565
- [513. 找树左下角的值](./day18/lc513.md)
6666
- [112. 路径总和](./day18/lc112.md)
67-
- [106. 从中序与后序遍历序列构造二叉树](./day18/lc106.md)
67+
- [106. 从中序与后序遍历序列构造二叉树](./day18/lc106.md)
68+
- [day 19](./day19.md)
69+
- [day 20](./day20.md)
70+
- [654. 最大二叉树](./day20/lc654.md)
71+
- [617. 合并二叉树](./day20/lc617.md)
72+
- [700.二叉搜索树中的搜索](./day20/lc700.md)
73+
- [98. 验证二叉搜索树](./day20/lc98.md)

notes/src/day19.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 休息日
2+
3+
corctf 还是一题也做不出 我没用

notes/src/day20.md

Whitespace-only changes.

notes/src/day20/lc617.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 617. 合并二叉树
2+
3+
4+
## 题目描述
5+
6+
给你两棵二叉树: root1 和 root2 。
7+
8+
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
9+
10+
返回合并后的二叉树。
11+
12+
注意: 合并过程必须从两个树的根节点开始。
13+
## 解题思路
14+
15+
16+
```cpp
17+
class Solution {
18+
public:
19+
TreeNode* mergeTrees(TreeNode* r1, TreeNode* r2) {
20+
if(!r1&&!r2)return 0;
21+
TreeNode*l=mergeTrees(r1?r1->left:0,r2?r2->left:0);
22+
TreeNode*r=mergeTrees(r1?r1->right:0,r2?r2->right:0);
23+
return new TreeNode((r1?r1->val:0)+(r2?r2->val:0),l,r);
24+
}
25+
};
26+
```
27+
## 学习感想

notes/src/day20/lc654.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 654. 最大二叉树
2+
3+
## 题目描述
4+
5+
给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
6+
7+
创建一个根节点,其值为 nums 中的最大值。
8+
递归地在最大值 左边 的 子数组前缀上 构建左子树。
9+
递归地在最大值 右边 的 子数组后缀上 构建右子树。
10+
返回 nums 构建的 最大二叉树 。
11+
12+
13+
14+
## 解题思路
15+
16+
17+
```cpp
18+
class Solution {
19+
public:
20+
TreeNode* constructMaximumBinaryTree(vector<int> nums) {
21+
if(!nums.size())return NULL;
22+
int m=-1;for(int i=0;i<nums.size();i++)m=nums[i]>m?nums[i]:m;
23+
auto i=find(nums.begin(),nums.end(),m);
24+
TreeNode*l=constructMaximumBinaryTree(vector<int>(nums.begin(),i));
25+
TreeNode*r=constructMaximumBinaryTree(vector<int>(i+1,nums.end()));
26+
return new TreeNode(*i,l,r);
27+
}
28+
};
29+
30+
```
31+
## 学习感想

notes/src/day20/lc700.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 700. 二叉搜索树中的搜索
2+
3+
4+
## 题目描述
5+
6+
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
7+
8+
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
9+
10+
11+
## 解题思路
12+
13+
14+
```cpp
15+
class Solution {
16+
public:
17+
TreeNode* searchBST(TreeNode* r, int val) {
18+
if(!r)return 0;
19+
if (r->val==val)return r;
20+
if (val<r->val)return searchBST(r->left,val);
21+
return searchBST(r->right,val);
22+
}
23+
};
24+
```
25+
26+
27+
## 学习感想

notes/src/day20/lc98.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 98. 验证二叉搜索树
2+
3+
## 题目描述
4+
5+
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
6+
7+
有效 二叉搜索树定义如下:
8+
9+
节点的左子树只包含 小于 当前节点的数。
10+
节点的右子树只包含 大于 当前节点的数。
11+
所有左子树和右子树自身必须也是二叉搜索树。
12+
13+
## 解题思路
14+
15+
16+
```cpp
17+
class Solution {
18+
public:
19+
bool f(TreeNode*r,long long l,long long ri){
20+
if(!r)return 1;
21+
if(r->val>=ri||r->val<=l)return 0;
22+
return f(r->left,l,r->val)&&f(r->right,r->val,ri);
23+
}
24+
bool isValidBST(TreeNode* r) {
25+
return f(r,(long long)(-2147483648)-1,(long long)2147483647+1);
26+
}
27+
};
28+
29+
```
30+
## 学习感想
31+
32+
int 范围好坑啊
33+
34+
`f(r,-2147483648-1,2147483647+1); ` 这样不会自动类型转换成longlong

0 commit comments

Comments
 (0)