|
| 1 | +use crate::TreeNode; |
| 2 | +use std::cell::RefCell; |
| 3 | +use std::rc::Rc; |
| 4 | +pub struct Solution {} |
| 5 | +impl Solution { |
| 6 | + pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { |
| 7 | + fn rec(root: Option<Rc<RefCell<TreeNode>>>, min: i64, max: i64) -> bool { |
| 8 | + if let Some(node) = root { |
| 9 | + let node = node.borrow(); |
| 10 | + if (node.val as i64) > min && (node.val as i64) < max { |
| 11 | + return rec(node.left.clone(), min, node.val.into()) |
| 12 | + && rec(node.right.clone(), node.val.into(), max); |
| 13 | + } else { |
| 14 | + return false; |
| 15 | + } |
| 16 | + } |
| 17 | + true |
| 18 | + } |
| 19 | + rec(root, i64::MIN, i64::MAX) |
| 20 | + } |
| 21 | +} |
0 commit comments