From a9039c0d2dd2c625c98009998677b20089b46524 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:39:36 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20404.=E5=B7=A6?= =?UTF-8?q?=E5=8F=B6=E5=AD=90=E4=B9=8B=E5=92=8C=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index aa2868df..c1ad602d 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -16,9 +16,9 @@ ![404.左叶子之和1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204151927654.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -48,7 +48,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) } ``` -## 递归法 +### 递归法 递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。 @@ -131,11 +131,11 @@ public: return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); } }; -``` +``` 精简之后的代码其实看不出来用的是什么遍历方式了,对于算法初学者以上根据第一个版本来学习。 -## 迭代法 +### 迭代法 本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:迭代法统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中的写法,可以写出一个前序遍历的迭代法。 @@ -177,7 +177,7 @@ public: ## 其他语言版本 -### Java +### Java: **递归** @@ -246,7 +246,7 @@ class Solution { ``` -### Python +### Python: 递归 ```python # Definition for a binary tree node. @@ -316,7 +316,7 @@ class Solution: ``` -### Go +### Go: **递归法** @@ -368,7 +368,7 @@ func sumOfLeftLeaves(root *TreeNode) int { ``` -### JavaScript +### JavaScript: **递归法** @@ -417,7 +417,7 @@ var sumOfLeftLeaves = function(root) { }; ``` -### TypeScript +### TypeScript: > 递归法 @@ -462,7 +462,7 @@ function sumOfLeftLeaves(root: TreeNode | null): number { }; ``` -### Swift +### Swift: **递归法** ```swift @@ -511,7 +511,7 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int { } ``` -### C +### C: 递归法: ```c int sumOfLeftLeaves(struct TreeNode* root){ @@ -561,7 +561,7 @@ int sumOfLeftLeaves(struct TreeNode* root){ } ``` -### Scala +### Scala: **递归:** ```scala @@ -600,7 +600,7 @@ object Solution { } ``` -### Rust +### Rust: **递归** @@ -656,3 +656,4 @@ impl Solution { + From f3a370113058a82be88164ddeabf5443f82f62a0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:41:28 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20513.=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E5=B7=A6=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 743b0df9..7ef934cc 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -20,9 +20,9 @@ ![513.找树左下角的值1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210204153017586.png) -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[怎么找二叉树的左下角? 递归中又带回溯了,怎么办?| LeetCode:513.找二叉树左下角的值](https://www.bilibili.com/video/BV1424y1Z7pn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -614,7 +614,7 @@ object Solution { } ``` -### rust +### Rust **层序遍历** @@ -689,3 +689,4 @@ impl Solution { + From bf6c4e7f7b3c8f118960362ab735f8e4977e0e58 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 17:53:29 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200112.=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E6=80=BB=E5=92=8C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 75 ++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 39285a3b..240462b6 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -21,9 +21,9 @@ 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -32,8 +32,8 @@ 那么接下来我通过详细讲解如下两道题,来回答这个问题: -* 112.路径总和 -* 113.路径总和ii +* [112.路径总和](https://leetcode.cn/problems/path-sum/) +* [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/) 这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。 @@ -218,7 +218,9 @@ public: 如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。 -# 113. 路径总和ii +## 相关题目推荐 + +### 113. 路径总和ii [力扣题目链接](https://leetcode.cn/problems/path-sum-ii/) @@ -232,7 +234,7 @@ public: ![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) -## 思路 +### 思路 113.路径总和ii要遍历整个树,找到所有路径,**所以递归函数不要返回值!** @@ -289,7 +291,7 @@ public: 至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。 -## 总结 +### 总结 本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。 @@ -300,11 +302,11 @@ public: -# 其他语言版本 +## 其他语言版本 -## java +### Java -### 0112.路径总和 +#### 0112.路径总和 ```java class solution { @@ -422,7 +424,7 @@ class solution { } ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii ```java class solution { @@ -529,9 +531,9 @@ class Solution { } ``` -## python +### Python -### 0112.路径总和 +#### 0112.路径总和 (版本一) 递归 ```python @@ -618,7 +620,7 @@ class Solution: -### 0113.路径总和-ii +#### 0113.路径总和-ii (版本一) 递归 ```python @@ -719,9 +721,9 @@ class Solution: ``` -## go +### Go -### 112. 路径总和 +#### 112. 路径总和 ```go //递归法 @@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -### 113. 路径总和 II +#### 113. 路径总和 II ```go /** @@ -786,9 +788,9 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { } ``` -## javascript +### Javascript -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -852,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -### 0113.路径总和-ii +#### 0113.路径总和-ii **递归** @@ -950,9 +952,9 @@ let pathSum = function(root, targetSum) { }; ``` -## TypeScript +### TypeScript -### 0112.路径总和 +#### 0112.路径总和 **递归法:** @@ -1034,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -### 0112.路径总和 ii +#### 0112.路径总和 ii **递归法:** @@ -1070,9 +1072,9 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { }; ``` -## Swift +### Swift -### 0112.路径总和 +#### 0112.路径总和 **递归** @@ -1141,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归** @@ -1192,10 +1194,10 @@ func traversal(_ cur: TreeNode?, count: Int) { } ``` -## C +### C -> 0112.路径总和 -> 递归法: +#### 0112.路径总和 +递归法: ```c bool hasPathSum(struct TreeNode* root, int targetSum){ @@ -1252,7 +1254,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -> 0113.路径总和 II +#### 0113.路径总和 II ```c int** ret; @@ -1317,9 +1319,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur } ``` -## Scala +### Scala -### 0112.路径总和 +#### 0112.路径总和 **递归:** @@ -1369,7 +1371,7 @@ object Solution { } ``` -### 0113.路径总和 II +#### 0113.路径总和 II **递归:** @@ -1405,9 +1407,9 @@ object Solution { } ``` -## rust +### Rust -### 112.路径总和.md +#### 0112.路径总和 递归: @@ -1461,7 +1463,7 @@ impl Solution { } ``` -### 113.路径总和-ii +#### 0113.路径总和-ii ```rust impl Solution { @@ -1514,3 +1516,4 @@ impl Solution { + From fe9323900758c0f056299728d1551ab57d1efab3 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:00:07 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200106.=E4=BB=8E?= =?UTF-8?q?=E4=B8=AD=E5=BA=8F=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 40 +++++++++---------- problems/0112.路径总和.md | 38 +++++++++--------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index a0bab999..0144fc5c 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -29,9 +29,9 @@ ![106. 从中序与后序遍历序列构造二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154316774.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder); 完整代码如下: -### C++完整代码 - ```CPP class Solution { private: @@ -281,8 +279,6 @@ public: 下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割) -### C++优化版本 - ```CPP class Solution { private: @@ -400,8 +396,9 @@ public: }; ``` +## 相关题目推荐 -# 105.从前序与中序遍历序列构造二叉树 +### 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -418,7 +415,7 @@ public: ![105. 从前序与中序遍历序列构造二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203154626672.png) -## 思路 +### 思路 本题和106是一样的道理。 @@ -547,7 +544,7 @@ public: }; ``` -# 思考题 +## 思考题 前序和中序可以唯一确定一棵二叉树。 @@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 所以前序和后序不能唯一确定一棵二叉树! -# 总结 +## 总结 之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。 @@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 106.从中序与后序遍历序列构造二叉树 @@ -688,7 +685,7 @@ class Solution { } ``` -## Python +### Python 105.从前序与中序遍历序列构造二叉树 @@ -754,7 +751,7 @@ class Solution: return root ``` -## Go +### Go 106 从中序与后序遍历序列构造二叉树 @@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode { ``` - - -## JavaScript +### JavaScript ```javascript var buildTree = function(inorder, postorder) { @@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) { }; ``` -## TypeScript +### TypeScript > 106.从中序与后序遍历序列构造二叉树 @@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null { }; ``` -## C +### C 106 从中序与后序遍历序列构造二叉树 @@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in } ``` -## Swift +### Swift 105 从前序与中序遍历序列构造二叉树 @@ -1140,7 +1135,7 @@ class Solution_0106 { } ``` -## Scala +### Scala 106 从中序与后序遍历序列构造二叉树 @@ -1188,7 +1183,7 @@ object Solution { } ``` -## rust +### Rust 106 从中序与后序遍历序列构造二叉树 @@ -1238,3 +1233,4 @@ impl Solution { + diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 240462b6..be03f719 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -306,7 +306,7 @@ public: ### Java -#### 0112.路径总和 +0112.路径总和 ```java class solution { @@ -424,7 +424,7 @@ class solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```java class solution { @@ -533,7 +533,7 @@ class Solution { ### Python -#### 0112.路径总和 +0112.路径总和 (版本一) 递归 ```python @@ -620,7 +620,7 @@ class Solution: -#### 0113.路径总和-ii +0113.路径总和-ii (版本一) 递归 ```python @@ -723,7 +723,7 @@ class Solution: ``` ### Go -#### 112. 路径总和 +112. 路径总和 ```go //递归法 @@ -748,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool { } ``` -#### 113. 路径总和 II +113. 路径总和 II ```go /** @@ -790,7 +790,7 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) { ### Javascript -#### 0112.路径总和 +0112.路径总和 **递归** @@ -854,7 +854,7 @@ let hasPathSum = function(root, targetSum) { }; ``` -#### 0113.路径总和-ii +0113.路径总和-ii **递归** @@ -954,7 +954,7 @@ let pathSum = function(root, targetSum) { ### TypeScript -#### 0112.路径总和 +0112.路径总和 **递归法:** @@ -1036,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean { }; ``` -#### 0112.路径总和 ii +0112.路径总和 ii **递归法:** @@ -1074,7 +1074,7 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] { ### Swift -#### 0112.路径总和 +0112.路径总和 **递归** @@ -1143,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归** @@ -1196,7 +1196,8 @@ func traversal(_ cur: TreeNode?, count: Int) { ### C -#### 0112.路径总和 +0112.路径总和 + 递归法: ```c @@ -1254,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){ } ``` -#### 0113.路径总和 II +0113.路径总和 II ```c int** ret; @@ -1321,7 +1322,7 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur ### Scala -#### 0112.路径总和 +0112.路径总和 **递归:** @@ -1371,7 +1372,7 @@ object Solution { } ``` -#### 0113.路径总和 II +0113.路径总和 II **递归:** @@ -1409,7 +1410,7 @@ object Solution { ### Rust -#### 0112.路径总和 +0112.路径总和 递归: @@ -1463,7 +1464,7 @@ impl Solution { } ``` -#### 0113.路径总和-ii +0113.路径总和-ii ```rust impl Solution { @@ -1516,4 +1517,3 @@ impl Solution { - From b4eefe985945fdeaf4e1e34ebbc90b74258d43e2 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 18:54:16 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0654.最大二叉树.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index 33a9176e..77d7980f 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -25,9 +25,9 @@ 给定的数组的大小在 [1, 1000] 之间。 -## 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树](https://www.bilibili.com/video/BV1MG411G7ox),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -310,7 +310,7 @@ class Solution: def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode: return self.traversal(nums, 0, len(nums)) - ``` +``` (版本三) 使用切片 @@ -587,3 +587,4 @@ impl Solution { + From eadc704c38816cfdb7f21f22d3489dc38ea5aecc Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:18:53 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200617.=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0617.合并二叉树.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/problems/0617.合并二叉树.md b/problems/0617.合并二叉树.md index 18a245c4..44092aae 100644 --- a/problems/0617.合并二叉树.md +++ b/problems/0617.合并二叉树.md @@ -19,9 +19,9 @@ 注意: 合并必须从两个树的根节点开始。 -# 视频讲解 +# 算法公开课 -**《代码随想录》算法视频公开课:[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树](https://www.bilibili.com/video/BV1m14y1Y7JK),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -164,7 +164,7 @@ public: }; ``` -## 迭代法 +### 迭代法 使用迭代法,如何同时处理两棵树呢? @@ -716,7 +716,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -793,4 +793,3 @@ impl Solution { - From ac239bc437cfa8de6859fa8a98cc04b9cf6d1d4c Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:20:25 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200700.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0700.二叉搜索树中的搜索.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/problems/0700.二叉搜索树中的搜索.md b/problems/0700.二叉搜索树中的搜索.md index 13064f97..bc21bab8 100644 --- a/problems/0700.二叉搜索树中的搜索.md +++ b/problems/0700.二叉搜索树中的搜索.md @@ -18,9 +18,9 @@ 在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索](https://www.bilibili.com/video/BV1wG411g7sF),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -415,7 +415,7 @@ object Solution { } ``` -### rust +### Rust 递归: @@ -469,3 +469,4 @@ impl Solution { + From 7746734d79d4726143375aaf1a8a8f835483346e Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:22:33 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200098.=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 95b657a5..a48ec065 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -20,18 +20,18 @@ ![98.验证二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20230310000750.png) -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。 有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。** -## 递归法 +### 递归法 可以递归中序遍历将二叉搜索树转变成一个数组,代码如下: @@ -211,7 +211,7 @@ public: 最后这份代码看上去整洁一些,思路也清晰。 -## 迭代法 +### 迭代法 可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html) @@ -245,7 +245,7 @@ public: 在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。 -# 总结 +## 总结 这道题目是一个简单题,但对于没接触过的同学还是有难度的。 @@ -254,10 +254,10 @@ public: 只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java //使用統一迭代法 @@ -369,7 +369,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一)利用中序递增性质,转换成数组 ```python @@ -479,7 +479,7 @@ class Solution: ``` -## Go +### Go ```Go func isValidBST(root *TreeNode) bool { @@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool { } ``` -## JavaScript +### JavaScript 辅助数组解决 @@ -595,7 +595,7 @@ var isValidBST = function (root) { }; ``` -## TypeScript +### TypeScript > 辅助数组解决: @@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean { }; ``` -## Scala +### Scala 辅助数组解决: ```scala @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -735,3 +735,4 @@ impl Solution { + From 74bbca292355dbc8ce9316329bfea503234ef0d6 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Fri, 21 Jul 2023 20:26:08 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200530.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E7=BB=9D=E5=AF=B9=E5=B7=AE=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0530.二叉搜索树的最小绝对差.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index 3e4391d6..56911858 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -19,12 +19,12 @@ 提示:树中至少有 2 个节点。 -# 视频讲解 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差](https://www.bilibili.com/video/BV1DD4y11779),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 题目中要求在二叉搜索树上任意两节点的差的绝对值的最小值。 @@ -32,7 +32,7 @@ 遇到在二叉搜索树上求什么最值啊,差值之类的,就把它想成在一个有序数组上求最值,求差值,这样就简单多了。 -## 递归 +### 递归 那么二叉搜索树采用中序遍历,其实就是一个有序数组。 @@ -102,7 +102,7 @@ public: 是不是看上去也并不复杂! -## 迭代 +### 迭代 看过这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式的写法就不能统一一下么?](https://programmercarl.com/二叉树的统一迭代法.html)文章之后,不难写出两种中序遍历的迭代法。 @@ -135,7 +135,7 @@ public: }; ``` -# 总结 +## 总结 **遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。** @@ -145,10 +145,10 @@ public: -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归 ```java @@ -235,7 +235,7 @@ class Solution { } } ``` -## Python +### Python 递归法(版本一)利用中序递增,结合数组 ```python @@ -313,7 +313,7 @@ class Solution: ``` -## Go: +### Go 中序遍历,然后计算最小差值 ```go @@ -340,7 +340,7 @@ func getMinimumDifference(root *TreeNode) int { } ``` -## JavaScript +### JavaScript 递归 先转换为有序数组 ```javascript /** @@ -415,7 +415,7 @@ var getMinimumDifference = function(root) { } ``` -## TypeScript +### TypeScript > 辅助数组解决 @@ -482,7 +482,7 @@ function getMinimumDifference(root: TreeNode | null): number { }; ``` -## Scala +### Scala 构建二叉树的有序数组: @@ -561,7 +561,7 @@ object Solution { } ``` -## rust +### Rust 构建二叉树的有序数组: @@ -652,3 +652,4 @@ impl Solution { + From 55b85b5251c7b4f2060795070341eaf4d97347d0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 17:57:10 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200501.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E4=BC=97?= =?UTF-8?q?=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0501.二叉搜索树中的众数.md | 35 ++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 1da32343..efbabc4a 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -33,20 +33,20 @@ 进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数](https://www.bilibili.com/video/BV1fD4y117gp),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目呢,递归法我从两个维度来讲。 首先如果不是二叉搜索树的话,应该怎么解题,是二叉搜索树,又应该如何解题,两种方式做一个比较,可以加深大家对二叉树的理解。 -## 递归法 +### 递归法 -### 如果不是二叉搜索树 +#### 如果不是二叉搜索树 如果不是二叉搜索树,最直观的方法一定是把这个树都遍历了,用map统计频率,把频率排个序,最后取前面高频的元素的集合。 @@ -140,7 +140,7 @@ public: **所以如果本题没有说是二叉搜索树的话,那么就按照上面的思路写!** -### 是二叉搜索树 +#### 是二叉搜索树 **既然是搜索树,它中序遍历就是有序的**。 @@ -271,7 +271,7 @@ public: ``` -## 迭代法 +### 迭代法 只要把中序遍历转成迭代,中间节点的处理逻辑完全一样。 @@ -326,7 +326,7 @@ public: }; ``` -# 总结 +## 总结 本题在递归法中,我给出了如果是普通二叉树,应该怎么求众数。 @@ -345,10 +345,10 @@ public: > **需要强调的是 leetcode上的耗时统计是非常不准确的,看个大概就行,一样的代码耗时可以差百分之50以上**,所以leetcode的耗时统计别太当回事,知道理论上的效率优劣就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 暴力法 @@ -472,7 +472,7 @@ class Solution { } } ``` -統一迭代法 +统一迭代法 ```Java class Solution { public int[] findMode(TreeNode root) { @@ -526,7 +526,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一)利用字典 @@ -640,7 +640,7 @@ class Solution: return result ``` -## Go +### Go 计数法,不使用额外空间,利用二叉树性质,中序遍历 ```go @@ -676,7 +676,7 @@ func findMode(root *TreeNode) []int { } ``` -## JavaScript +### JavaScript 使用额外空间map的方法 ```javascript @@ -753,7 +753,7 @@ var findMode = function(root) { }; ``` -## TypeScript +### TypeScript > 辅助Map法 @@ -852,7 +852,7 @@ function findMode(root: TreeNode | null): number[] { }; ``` -## Scala +### Scala 暴力: ```scala @@ -923,7 +923,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -1015,3 +1015,4 @@ pub fn find_mode(root: Option>>) -> Vec { + From 3a2490b92fe00b5e2b6acb602f33328b48e426ad Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:41:57 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200236.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0236.二叉树的最近公共祖先.md | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index 0ebd5566..9db7409e 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -34,12 +34,12 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 遇到这个题目首先想的是要是能自底向上查找就好了,这样就可以找到公共祖先了。 @@ -226,7 +226,7 @@ public: }; ``` -# 总结 +## 总结 这道题目刷过的同学未必真正了解这里面回溯的过程,以及结果是如何一层一层传上去的。 @@ -243,10 +243,10 @@ public: 本题没有给出迭代法,因为迭代法不适合模拟回溯的过程。理解递归的解法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```Java class Solution { @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -312,7 +312,7 @@ class Solution: return left ``` -## Go +### Go ```Go func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { @@ -343,7 +343,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { } ``` -## JavaScript +### JavaScript ```javascript var lowestCommonAncestor = function(root, p, q) { @@ -370,7 +370,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript ```typescript function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { @@ -384,7 +384,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala ```scala object Solution { @@ -404,7 +404,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -436,3 +436,4 @@ impl Solution { + From 07e1ccca1925d16b5f9f58050d135d3a326b44af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:52:09 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200235.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...35.二叉搜索树的最近公共祖先.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 9777bb0b..2b8af060 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -36,11 +36,11 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉搜索树中。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。 @@ -71,7 +71,7 @@ 可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回! -## 递归法 +### 递归法 递归三部曲如下: @@ -203,7 +203,7 @@ public: }; ``` -## 迭代法 +### 迭代法 对于二叉搜索树的迭代法,大家应该在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)就了解了。 @@ -229,7 +229,7 @@ public: 灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕? -# 总结 +## 总结 对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。 @@ -238,10 +238,10 @@ public: 最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归法: ```java @@ -273,7 +273,7 @@ class Solution { ``` -## Python +### Python 递归法(版本一) ```python @@ -326,7 +326,7 @@ class Solution: ``` -## Go +### Go 递归法: ```go @@ -350,7 +350,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { ``` -## JavaScript +### JavaScript 递归法: ```javascript @@ -391,7 +391,7 @@ var lowestCommonAncestor = function(root, p, q) { }; ``` -## TypeScript +### TypeScript > 递归法: @@ -422,7 +422,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree }; ``` -## Scala +### Scala 递归: @@ -453,7 +453,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -519,3 +519,4 @@ impl Solution { + From 70271161fff40fb3ab1929dcb37b19e3efe469b0 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:54:12 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200701.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E6=93=8D=E4=BD=9C=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0701.二叉搜索树中的插入操作.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index fc4351ba..511d161c 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -23,11 +23,11 @@ * -10^8 <= val <= 10^8 * 新值和原始二叉搜索树中的任意节点值都不同 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。 @@ -43,7 +43,7 @@ 接下来就是遍历二叉搜索树的过程了。 -## 递归 +### 递归 递归三部曲: @@ -165,7 +165,7 @@ public: **网上千篇一律的代码,可能会误导大家认为通过递归函数返回节点 这样的写法是天经地义,其实这里是有优化的!** -## 迭代 +### 迭代 再来看看迭代法,对二叉搜索树迭代写法不熟悉,可以看这篇:[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html) @@ -198,7 +198,7 @@ public: }; ``` -# 总结 +## 总结 首先在二叉搜索树中的插入操作,大家不用恐惧其重构搜索树,其实根本不用重构。 @@ -207,9 +207,9 @@ public: 最后依然给出了迭代的方法,迭代的方法就需要记录当前遍历节点的父节点了,这个和没有返回值的递归函数实现的代码逻辑是一样的。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { @@ -254,7 +254,7 @@ class Solution { } ``` ----- -## Python +### Python 递归法(版本一) ```python @@ -371,7 +371,7 @@ class Solution: ``` ----- -## Go +### Go 递归法 @@ -417,7 +417,7 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode { } ``` ----- -## JavaScript +### JavaScript 有返回值的递归写法 @@ -530,7 +530,7 @@ var insertIntoBST = function (root, val) { }; ``` -## TypeScript +### TypeScript > 递归-有返回值 @@ -595,7 +595,7 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null { ``` -## Scala +### Scala 递归: @@ -632,7 +632,7 @@ object Solution { } ``` -### rust +### Rust 迭代: @@ -697,3 +697,4 @@ impl Solution { + From 0f22ada3df0468334a45059a037ec6a7fa198d85 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:56:18 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200450.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E8=8A=82=E7=82=B9=20=E6=8E=92=E7=89=88=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0450.删除二叉搜索树中的节点.md | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 3d73598d..13599fd8 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -24,15 +24,15 @@ ![450.删除二叉搜索树中的节点](https://code-thinking-1253855093.file.myqcloud.com/pics/20201020171048265.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。 -## 递归 +### 递归 递归三部曲: @@ -161,7 +161,7 @@ public: }; ``` -## 普通二叉树的删除方式 +### 普通二叉树的删除方式 这里我在介绍一种通用的删除,普通二叉树的删除方式(没有使用搜索树的特性,遍历整棵树),用交换值的操作来删除目标节点。 @@ -198,7 +198,7 @@ public: 这个代码是简短一些,思路也巧妙,但是不太好想,实操性不强,推荐第一种写法! -## 迭代法 +### 迭代法 删除节点的迭代法还是复杂一些的,但其本质我在递归法里都介绍了,最关键就是删除节点的操作(动画模拟的过程) @@ -246,7 +246,7 @@ public: }; ``` -# 总结 +## 总结 读完本篇,大家会发现二叉搜索树删除节点比增加节点复杂的多。 @@ -264,10 +264,10 @@ public: 迭代法其实不太容易写出来,所以如果是初学者的话,彻底掌握第一种递归写法就够了。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java ```java class Solution { public TreeNode deleteNode(TreeNode root, int key) { @@ -323,7 +323,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python class Solution: @@ -411,7 +411,7 @@ class Solution: return root ``` -## Go +### Go ```Go // 递归版本 func deleteNode(root *TreeNode, key int) *TreeNode { @@ -497,7 +497,7 @@ func deleteNode(root *TreeNode, key int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 @@ -588,7 +588,7 @@ var deleteNode = function (root, key) { } ``` -## TypeScript +### TypeScript > 递归法: @@ -652,7 +652,7 @@ function deleteNode(root: TreeNode | null, key: number): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -682,7 +682,7 @@ object Solution { } ``` -## rust +### Rust ```rust impl Solution { @@ -720,3 +720,4 @@ impl Solution { + From e14e2169c4c303d5e690705245230323774df795 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 18:58:10 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200669.=E4=BF=AE?= =?UTF-8?q?=E5=89=AA=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20?= =?UTF-8?q?=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0669.修剪二叉搜索树.md | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 85922a1d..2cfbfefc 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -20,17 +20,17 @@ ![669.修剪二叉搜索树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20201014173219142.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 相信看到这道题目大家都感觉是一道简单题(事实上leetcode上也标明是简单)。 但还真的不简单! -## 递归法 +### 递归法 直接想法就是:递归处理,然后遇到 `root->val < low || root->val > high` 的时候直接return NULL,一波修改,赶紧利落。 @@ -188,7 +188,7 @@ public: 只看代码,其实不太好理解节点是如何移除的,这一块大家可以自己再模拟模拟! -## 迭代法 +### 迭代法 因为二叉搜索树的有序性,不需要使用栈模拟递归的过程。 @@ -233,7 +233,7 @@ public: }; ``` -# 总结 +## 总结 修剪二叉搜索树其实并不难,但在递归法中大家可看出我费了很大的功夫来讲解如何删除节点的,这个思路其实是比较绕的。 @@ -243,10 +243,10 @@ public: 本题我依然给出递归法和迭代法,初学者掌握递归就可以了,如果想进一步学习,就把迭代法也写一写。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** @@ -311,7 +311,7 @@ class Solution { ```` -## Python +### Python 递归法(版本一) ```python @@ -381,7 +381,7 @@ class Solution: ``` -## Go +### Go ```go // 递归 @@ -436,7 +436,7 @@ func trimBST(root *TreeNode, low int, high int) *TreeNode { ``` -## JavaScript版本 +### JavaScript 迭代: @@ -492,7 +492,7 @@ var trimBST = function (root,low,high) { } ``` -## TypeScript +### TypeScript > 递归法 @@ -540,7 +540,7 @@ function trimBST(root: TreeNode | null, low: number, high: number): TreeNode | n }; ``` -## Scala +### Scala 递归法: @@ -557,7 +557,7 @@ object Solution { } ``` -## rust +### Rust // 递归 @@ -590,3 +590,4 @@ impl Solution { + From 572c08984f9ec9821383b3c9a51dbbf1684eb6af Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:00:08 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200108.=E5=B0=86?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...将有序数组转换为二叉搜索树.md | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 056ef3e2..e699005e 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -20,11 +20,11 @@ ![108.将有序数组转换为二叉搜索树](https://code-thinking-1253855093.file.myqcloud.com/pics/20201022164420763.png) -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 做这道题目之前大家可以了解一下这几道: @@ -71,7 +71,7 @@ **这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。 -## 递归 +### 递归 递归三部曲: @@ -155,7 +155,7 @@ public: **注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。 -## 迭代法 +### 迭代法 迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。 @@ -203,7 +203,7 @@ public: }; ``` -# 总结 +## 总结 **在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。 @@ -216,10 +216,10 @@ public: 最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java 递归: 左闭右开 [left,right) ```Java @@ -315,7 +315,7 @@ class Solution { } ``` -## Python +### Python 递归法 ```python class Solution: @@ -377,7 +377,7 @@ class Solution: ``` -## Go +### Go 递归(隐含回溯) @@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) { return root; }; ``` -## TypeScript +### TypeScript ```typescript function sortedArrayToBST(nums: number[]): TreeNode | null { @@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null { }; ``` -## C +### C 递归 ```c @@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { } ``` -## Scala +### Scala 递归: @@ -511,7 +511,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -536,3 +536,4 @@ impl Solution { + From 6bf34cd8ba3837d16013dd2ac78b5d632f3f2e22 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:02:05 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200538.=E5=B0=86?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=E7=B4=AF=E5=8A=A0=E6=A0=91=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...38.把二叉搜索树转换为累加树.md | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 781763a4..c403c98f 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -44,11 +44,11 @@ * 树中的所有值 互不相同 。 * 给定的树为二叉搜索树。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 -# 思路 +## 思路 一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。 @@ -64,7 +64,7 @@ 那么知道如何遍历这个二叉树,也就迎刃而解了,**从树中可以看出累加的顺序是右中左,所以我们需要反中序遍历这个二叉树,然后顺序累加就可以了**。 -## 递归 +### 递归 遍历顺序如图所示: @@ -131,7 +131,7 @@ public: }; ``` -## 迭代法 +### 迭代法 迭代法其实就是中序模板题了,在[二叉树:前中后序迭代法](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序统一方式迭代法](https://programmercarl.com/二叉树的统一迭代法.html)可以选一种自己习惯的写法。 @@ -166,17 +166,17 @@ public: }; ``` -# 总结 +## 总结 经历了前面各种二叉树增删改查的洗礼之后,这道题目应该比较简单了。 **好了,二叉树已经接近尾声了,接下来就是要对二叉树来一个大总结了**。 -# 其他语言版本 +## 其他语言版本 -## Java +### Java **递归** ```Java @@ -237,7 +237,7 @@ class Solution { } ``` -## Python +### Python 递归法(版本一) ```python # Definition for a binary tree node. @@ -318,7 +318,7 @@ class Solution: self.traversal(root) return root -``` +``` 迭代法(版本二) ```python class Solution: @@ -338,9 +338,9 @@ class Solution: pre = cur.val cur =cur.left return root -``` +``` -## Go +### Go 弄一个sum暂存其和值 ```go @@ -362,7 +362,7 @@ func traversal(cur *TreeNode) { } ``` -## JavaScript +### JavaScript 递归 ```javascript @@ -401,7 +401,7 @@ var convertBST = function (root) { }; ``` -##C +### C 递归 ```c @@ -422,7 +422,7 @@ struct TreeNode* convertBST(struct TreeNode* root){ } ``` -## TypeScript +### TypeScript > 递归法 @@ -462,7 +462,7 @@ function convertBST(root: TreeNode | null): TreeNode | null { }; ``` -## Scala +### Scala ```scala object Solution { @@ -481,7 +481,7 @@ object Solution { } ``` -## rust +### Rust 递归: @@ -535,3 +535,4 @@ impl Solution { + From 75390603854b899d5a164b0cf508ec393aaa3712 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Sun, 23 Jul 2023 19:04:13 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92=E7=89=88?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树总结篇.md | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/problems/二叉树总结篇.md b/problems/二叉树总结篇.md index 4c742a6b..82949543 100644 --- a/problems/二叉树总结篇.md +++ b/problems/二叉树总结篇.md @@ -92,7 +92,7 @@ * 递归:中序,双指针操作 * 迭代:模拟中序,逻辑相同 * [求二叉搜索树的众数](https://programmercarl.com/0501.二叉搜索树中的众数.html) - + * 递归:中序,清空结果集的技巧,遍历一遍便可求众数集合 * [二叉搜索树转成累加树](https://programmercarl.com/0538.把二叉搜索树转换为累加树.html) @@ -154,29 +154,13 @@ 这个图是 [代码随想录知识星球](https://programmercarl.com/other/kstar.html) 成员:[青](https://wx.zsxq.com/dweb2/index/footprint/185251215558842),所画,总结的非常好,分享给大家。 - **最后,二叉树系列就这么完美结束了,估计这应该是最长的系列了,感谢大家33天的坚持与陪伴,接下来我们又要开始新的系列了「回溯算法」!** - - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - -

+