diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 262ad18c..a925e36a 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -662,6 +662,32 @@ impl Solution { } ``` +辅助数组: + +```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); + } +} +``` +