From 5a2b7b4ecdfd020203ae35c70b321c25f1112daa Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:16:21 +0800 Subject: [PATCH 1/9] =?UTF-8?q?Update=200235.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=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.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充B站视频链接 --- problems/0235.二叉搜索树的最近公共祖先.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index c90b3d8c..7e132346 100644 --- a/problems/0235.二叉搜索树的最近公共祖先.md +++ b/problems/0235.二叉搜索树的最近公共祖先.md @@ -35,8 +35,11 @@ * 所有节点的值都是唯一的。 * p、q 为不同节点且均存在于给定的二叉搜索树中。 -# 思路 +# 算法公开课 +**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + +# 思路 做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。 From 8ecfbb79637cda0a9cbd8f236572a60184880e27 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:24:04 +0800 Subject: [PATCH 2/9] =?UTF-8?q?Update=200701.=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E6=8F=92=E5=85=A5=E6=93=8D?= =?UTF-8?q?=E4=BD=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充B站视频链接 --- problems/0701.二叉搜索树中的插入操作.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 2e899e22..2558743c 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -22,6 +22,10 @@ * -10^8 <= val <= 10^8 * 新值和原始二叉搜索树中的任意节点值都不同 +# 算法公开课 + +**《代码随想录》算法视频公开课:[原来这么简单? | LeetCode:701.二叉搜索树中的插入操作](https://www.bilibili.com/video/BV1Et4y1c78Y?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 其实这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。 From e588422356e053e927f24a7667c7b1f4dad88fd8 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:26:02 +0800 Subject: [PATCH 3/9] =?UTF-8?q?Update=200450.=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充B站视频链接 --- problems/0450.删除二叉搜索树中的节点.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index d178596e..f6621035 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -23,6 +23,10 @@ ![450.删除二叉搜索树中的节点](https://img-blog.csdnimg.cn/20201020171048265.png) +# 算法公开课 + +**《代码随想录》算法视频公开课:[调整二叉树的结构最难!| LeetCode:450.删除二叉搜索树中的节点](https://www.bilibili.com/video/BV1tP41177us?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心里准备。 From d0758dce9e3328633bc3bff9aeacea6c312ba6b1 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:27:43 +0800 Subject: [PATCH 4/9] =?UTF-8?q?Update=200669.=E4=BF=AE=E5=89=AA=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补充B站视频链接 --- problems/0669.修剪二叉搜索树.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index 98af3c26..09959719 100644 --- a/problems/0669.修剪二叉搜索树.md +++ b/problems/0669.修剪二叉搜索树.md @@ -18,6 +18,10 @@ ![669.修剪二叉搜索树1](https://img-blog.csdnimg.cn/20201014173219142.png) +# 算法公开课 + +**《代码随想录》算法视频公开课:[你修剪的方式不对,我来给你纠正一下!| LeetCode:669. 修剪二叉搜索树](https://www.bilibili.com/video/BV17P41177ud?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 相信看到这道题目大家都感觉是一道简单题(事实上leetcode上也标明是简单)。 From 0ebb8dc5415e0913e7b0fc184d14abe178609502 Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:28:56 +0800 Subject: [PATCH 5/9] =?UTF-8?q?Update=200108.=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0108.将有序数组转换为二叉搜索树.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index 9318a0e2..57080855 100644 --- a/problems/0108.将有序数组转换为二叉搜索树.md +++ b/problems/0108.将有序数组转换为二叉搜索树.md @@ -19,6 +19,10 @@ ![108.将有序数组转换为二叉搜索树](https://img-blog.csdnimg.cn/20201022164420763.png) +# 算法公开课 + +**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 做这道题目之前大家可以了解一下这几道: From 51ab1648607b991cf58e5aef018410467201af2a Mon Sep 17 00:00:00 2001 From: Flow-sandyu <72751523+Flow-sandyu@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:29:47 +0800 Subject: [PATCH 6/9] =?UTF-8?q?Update=200538.=E6=8A=8A=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E8=BD=AC=E6=8D=A2=E4=B8=BA=E7=B4=AF?= =?UTF-8?q?=E5=8A=A0=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0538.把二叉搜索树转换为累加树.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 823771b1..c9c8a2e7 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -43,6 +43,10 @@ * 树中的所有值 互不相同 。 * 给定的树为二叉搜索树。 +# 算法公开课 + +**《代码随想录》算法视频公开课:[普大喜奔!二叉树章节已全部更完啦!| LeetCode:538.把二叉搜索树转换为累加树](https://www.bilibili.com/video/BV1d44y1f7wP?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后在遍历其他节点累加?怎么一想这么麻烦呢。 From 8d04e8a3804b1dd7538104cb9953451109256976 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:08:58 +0800 Subject: [PATCH 7/9] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index bb38ae35..262ad18c 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -639,6 +639,29 @@ object Solution { } ``` +## rust + +递归: + +```rust +impl Solution { + pub fn is_valid_bst(root: Option>>) -> bool { + Self::valid_bst(i64::MIN, i64::MAX, root) + } + pub fn valid_bst(low: i64, upper: i64, root: Option>>) -> bool { + if root.is_none() { + return true; + } + let root = root.as_ref().unwrap().borrow(); + if root.val as i64 <= low || root.val as i64 >= upper { + return false; + } + Self::valid_bst(low, root.val as i64, root.left.clone()) + && Self::valid_bst(root.val as i64, upper, root.right.clone()) + } +} +``` +

From c230c2d871b2b9a93fbc137b17fb5ef8b1eea48a Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Mon, 28 Nov 2022 17:23:00 +0800 Subject: [PATCH 8/9] =?UTF-8?q?Update=200098.=E9=AA=8C=E8=AF=81=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0098.验证二叉搜索树.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 262ad18c..a925e36a 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -662,6 +662,32 @@ impl Solution { } ``` +辅助数组: + +```rust +impl Solution { + pub fn is_valid_bst(root: Option>>) -> bool { + let mut vec = vec![]; + Self::valid_bst(root, &mut vec); + for i in 1..vec.len() { + if vec[i] <= vec[i - 1] { + return false; + } + } + true + } + pub fn valid_bst(root: Option>>, mut v: &mut Vec) { + if root.is_none() { + return; + } + let node = root.as_ref().unwrap().borrow(); + Self::valid_bst(node.left.clone(), v); + v.push(node.val as i64); + Self::valid_bst(node.right.clone(), v); + } +} +``` +

From bfced8f38571131d3c8fc982ba633c90fe02114e Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+fwqaaq@users.noreply.github.com> Date: Sat, 17 Dec 2022 00:17:30 +0800 Subject: [PATCH 9/9] =?UTF-8?q?update=200077.=E7=BB=84=E5=90=88.md=20about?= =?UTF-8?q?=20rust=20=E5=91=BD=E5=90=8D=E4=BB=A5=E5=8F=8A=E5=85=B6?= =?UTF-8?q?=E5=AE=83=E6=96=B9=E9=9D=A2=E7=9A=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 854b302b..77ea5e80 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -514,21 +514,21 @@ function combine(n: number, k: number): number[][] { ```Rust impl Solution { - fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, start_index: i32) { let len= path.len() as i32; if len == k{ result.push(path.to_vec()); return; } - for i in startIndex..= n { + for i in start_index..= n { path.push(i); Self::backtracking(result, path, n, k, i+1); path.pop(); } } pub fn combine(n: i32, k: i32) -> Vec> { - let mut result: Vec> = Vec::new(); - let mut path: Vec = Vec::new(); + let mut result = vec![]; + let mut path = vec![]; Self::backtracking(&mut result, &mut path, n, k, 1); result } @@ -538,22 +538,22 @@ impl Solution { 剪枝 ```Rust impl Solution { - fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, startIndex: i32) { + fn backtracking(result: &mut Vec>, path: &mut Vec, n: i32, k: i32, start_index: i32) { let len= path.len() as i32; if len == k{ result.push(path.to_vec()); return; } // 此处剪枝 - for i in startIndex..= n - (k - len) + 1 { + for i in start_index..= n - (k - len) + 1 { path.push(i); Self::backtracking(result, path, n, k, i+1); path.pop(); } } pub fn combine(n: i32, k: i32) -> Vec> { - let mut result: Vec> = Vec::new(); - let mut path: Vec = Vec::new(); + let mut result = vec![]; + let mut path = vec![]; Self::backtracking(&mut result, &mut path, n, k, 1); result }