记rust的Option<Rc<RefCell<TreeNode>>>例子

// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }

use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
    pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
        Self::dfs(root, 0, 0, false, false)
    }

    fn dfs(root: Option<Rc<RefCell<TreeNode>>>, lo:i32, hi:i32, l:bool, u:bool) -> bool {
        if let Some(root) = root {
            let root = root.borrow();
            if (l && root.val <= lo) || (u && root.val >= hi) {return false;}
            return Self::dfs(root.left.clone(), lo, root.val, l, true) && Self::dfs(root.right.clone(), root.val, hi, true, u);
        } else {
            return true;
        }
    }
}
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
    pub fn is_valid_bst(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
        let mut vec = vec![];
        Solution::preorder_traverse(root.as_ref(), &mut vec)
    }

    fn preorder_traverse(
        root: Option<&Rc<RefCell<TreeNode>>>,
        formers: &mut Vec<(i32, i32)>,
    ) -> bool {
        if let Some(node) = root {
            let root_val = root.as_ref().unwrap().borrow().val;
            for former in formers.iter() {
                if (former.0 < 0 && root_val >= former.1) || (former.0 > 0 && root_val <= former.1)
                {
                    return false;
                }
            }
            let mut to_right = formers.clone();
            formers.push((-1, root_val));
            to_right.push((1, root_val));
            Solution::preorder_traverse(node.borrow().left.as_ref(), formers)
                && Solution::preorder_traverse(node.borrow().right.as_ref(), &mut to_right)
        } else {
            true
        }
    }
}
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
// 
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//     TreeNode {
//       val,
//       left: None,
//       right: None
//     }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
    pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
        if nums.is_empty() {return None;}
        let mid = nums.len()/2;
        let mut root:TreeNode = TreeNode::new(nums[mid]);
        let le = nums[..mid].to_vec();
        let ri = nums[mid+1..].to_vec();
        root.left = Self::sorted_array_to_bst(le);
        root.right = Self::sorted_array_to_bst(ri);
        Some(Rc::new(RefCell::new(root)))
        // let root = Rc::new(RefCell::new(TreeNode::new(nums[mid])));
        // root.borrow_mut().left =
        //     Self::sorted_array_to_bst(nums[..mid].to_vec());
        // root.borrow_mut().right = Self::sorted_array_to_bst(nums[mid + 1..].to_vec());
        // Some(root)
    }
}