标签:use efi ret tree dfs struct style line amp
1 // Definition for a binary tree node. 2 // #[derive(Debug, PartialEq, Eq)] 3 // pub struct TreeNode { 4 // pub val: i32, 5 // pub left: Option<Rc<RefCell<TreeNode>>>, 6 // pub right: Option<Rc<RefCell<TreeNode>>>, 7 // } 8 // 9 // impl TreeNode { 10 // #[inline] 11 // pub fn new(val: i32) -> Self { 12 // TreeNode { 13 // val, 14 // left: None, 15 // right: None 16 // } 17 // } 18 // } 19 20 use std::cell::RefCell; 21 use std::rc::Rc; 22 impl Solution { 23 pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { 24 Self::dfs(root, 0, 0, false, false) 25 } 26 27 fn dfs(root: Option<Rc<RefCell<TreeNode>>>, lo:i32, hi:i32, l:bool, u:bool) -> bool { 28 if let Some(root) = root { 29 let root = root.borrow(); 30 if (l && root.val <= lo) || (u && root.val >= hi) {return false;} 31 return Self::dfs(root.left.clone(), lo, root.val, l, true) && Self::dfs(root.right.clone(), root.val, hi, true, u); 32 } else { 33 return true; 34 } 35 } 36 }
1 use std::cell::RefCell; 2 use std::rc::Rc; 3 impl Solution { 4 pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool { 5 let mut vec = vec![]; 6 Solution::preorder_traverse(root.as_ref(), &mut vec) 7 } 8 9 fn preorder_traverse( 10 root: Option<&Rc<RefCell<TreeNode>>>, 11 formers: &mut Vec<(i32, i32)>, 12 ) -> bool { 13 if let Some(node) = root { 14 let root_val = root.as_ref().unwrap().borrow().val; 15 for former in formers.iter() { 16 if (former.0 < 0 && root_val >= former.1) || (former.0 > 0 && root_val <= former.1) 17 { 18 return false; 19 } 20 } 21 let mut to_right = formers.clone(); 22 formers.push((-1, root_val)); 23 to_right.push((1, root_val)); 24 Solution::preorder_traverse(node.borrow().left.as_ref(), formers) 25 && Solution::preorder_traverse(node.borrow().right.as_ref(), &mut to_right) 26 } else { 27 true 28 } 29 } 30 }
1 // Definition for a binary tree node. 2 // #[derive(Debug, PartialEq, Eq)] 3 // pub struct TreeNode { 4 // pub val: i32, 5 // pub left: Option<Rc<RefCell<TreeNode>>>, 6 // pub right: Option<Rc<RefCell<TreeNode>>>, 7 // } 8 // 9 // impl TreeNode { 10 // #[inline] 11 // pub fn new(val: i32) -> Self { 12 // TreeNode { 13 // val, 14 // left: None, 15 // right: None 16 // } 17 // } 18 // } 19 use std::rc::Rc; 20 use std::cell::RefCell; 21 impl Solution { 22 pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> { 23 if nums.is_empty() {return None;} 24 let mid = nums.len()/2; 25 let mut root:TreeNode = TreeNode::new(nums[mid]); 26 let le = nums[..mid].to_vec(); 27 let ri = nums[mid+1..].to_vec(); 28 root.left = Self::sorted_array_to_bst(le); 29 root.right = Self::sorted_array_to_bst(ri); 30 Some(Rc::new(RefCell::new(root))) 31 // let root = Rc::new(RefCell::new(TreeNode::new(nums[mid]))); 32 // root.borrow_mut().left = 33 // Self::sorted_array_to_bst(nums[..mid].to_vec()); 34 // root.borrow_mut().right = Self::sorted_array_to_bst(nums[mid + 1..].to_vec()); 35 // Some(root) 36 } 37 }
记rust的Option<Rc<RefCell<TreeNode>>>例子
标签:use efi ret tree dfs struct style line amp
原文地址:https://www.cnblogs.com/chenguifeng/p/12364843.html