Update 0098.验证二叉搜索树.md

This commit is contained in:
fw_qaq
2022-11-28 17:08:58 +08:00
committed by GitHub
parent d610b113e8
commit 8d04e8a380

View File

@ -639,6 +639,29 @@ object Solution {
}
```
## rust
递归:
```rust
impl Solution {
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
Self::valid_bst(i64::MIN, i64::MAX, root)
}
pub fn valid_bst(low: i64, upper: i64, root: Option<Rc<RefCell<TreeNode>>>) -> 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())
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>