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 } diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index db9c28ad..95ae783f 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -637,6 +637,55 @@ 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()) + } +} +``` + +辅助数组: + +```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); + } +} +``` +

diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md index ffb60b38..01c276fd 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),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 做这道题目之前大家可以了解一下这几道: diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md index 7f8f6422..c048c929 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,那么当前节点就是最近公共祖先。 diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 0b4048d5..a25b33ef 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),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 搜索树的节点删除要比节点增加复杂的多,有很多情况需要考虑,做好心理准备。 diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 9940e604..ec12c525 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),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 一看到累加树,相信很多小伙伴都会疑惑:如何累加?遇到一个节点,然后再遍历其他节点累加?怎么一想这么麻烦呢。 diff --git a/problems/0669.修剪二叉搜索树.md b/problems/0669.修剪二叉搜索树.md index c2d7b79b..1c5fdea1 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上也标明是简单)。 diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 432413b6..ebfe1390 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),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 + # 思路 这道题目其实是一道简单题目,**但是题目中的提示:有多种有效的插入方式,还可以重构二叉搜索树,一下子吓退了不少人**,瞬间感觉题目复杂了很多。