https://leetcode.cn/problems/shu-de-zi-jie-gou-lcof/

    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 is_sub_structure(a: Option<Rc<RefCell<TreeNode>>>, b: Option<Rc<RefCell<TreeNode>>>) -> bool {
    23. if a.is_none() || b.is_none() {
    24. return false;
    25. }
    26. // as_ref() 是为了获取Option内部的值的引用,可以暂时理解为解决所有权问题
    27. let a_borrow = a.as_ref().unwrap().borrow();// core::cell::Ref<TreeNode>
    28. Self::equal_tree(&a, &b) ||
    29. Self::is_sub_structure(a_borrow.left.clone(), b.clone()) ||
    30. Self::is_sub_structure(a_borrow.right.clone(), b.clone())
    31. }
    32. pub fn equal_tree(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
    33. match (a, b) {
    34. (Some(node_a), Some(node_b)) => {
    35. if node_a.borrow().val == node_b.borrow().val {
    36. return Self::equal_tree(&node_a.borrow().left, &node_b.borrow().left) &&
    37. Self::equal_tree(&node_a.borrow().right, &node_b.borrow().right);
    38. } else {
    39. return false;
    40. }
    41. },
    42. (None, Some(node_b)) => {
    43. return false;
    44. },
    45. (Some(node_a), None) => {
    46. return true;
    47. },
    48. (None, None) => return true
    49. }
    50. }
    51. }
    1. fn print_type_of<T>(_: &T) {
    2. println!("{}", std::any::type_name::<T>())
    3. }
    4. fn main() {
    5. let text: Option<RefCell<String>> = Some(RefCell::new("Hello, world!".to_string()));
    6. // text.as_ref();
    7. print_type_of(&text.as_ref().unwrap().borrow());
    8. }