mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-14 06:55:41 +08:00
Update 0098.验证二叉搜索树.md
This commit is contained in:
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user