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