https://leetcode.cn/problems/shu-de-zi-jie-gou-lcof/
// 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 is_sub_structure(a: Option<Rc<RefCell<TreeNode>>>, b: Option<Rc<RefCell<TreeNode>>>) -> bool {
if a.is_none() || b.is_none() {
return false;
}
// as_ref() 是为了获取Option内部的值的引用,可以暂时理解为解决所有权问题
let a_borrow = a.as_ref().unwrap().borrow();// core::cell::Ref<TreeNode>
Self::equal_tree(&a, &b) ||
Self::is_sub_structure(a_borrow.left.clone(), b.clone()) ||
Self::is_sub_structure(a_borrow.right.clone(), b.clone())
}
pub fn equal_tree(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
match (a, b) {
(Some(node_a), Some(node_b)) => {
if node_a.borrow().val == node_b.borrow().val {
return Self::equal_tree(&node_a.borrow().left, &node_b.borrow().left) &&
Self::equal_tree(&node_a.borrow().right, &node_b.borrow().right);
} else {
return false;
}
},
(None, Some(node_b)) => {
return false;
},
(Some(node_a), None) => {
return true;
},
(None, None) => return true
}
}
}
fn print_type_of<T>(_: &T) {
println!("{}", std::any::type_name::<T>())
}
fn main() {
let text: Option<RefCell<String>> = Some(RefCell::new("Hello, world!".to_string()));
// text.as_ref();
print_type_of(&text.as_ref().unwrap().borrow());
}