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

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

View File

@ -662,6 +662,32 @@ impl Solution {
} }
``` ```
辅助数组:
```rust
impl Solution {
pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> 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<Rc<RefCell<TreeNode>>>, mut v: &mut Vec<i64>) {
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);
}
}
```
<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"/>