题解 | #相同的二叉树# | Rust
相同的二叉树
https://www.nowcoder.com/practice/5a3b2cf4211249c89d6ced7987aeb775
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option<Box<TreeNode>>, * pub right: Option<Box<TreeNode>>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root1 TreeNode类 * @param root2 TreeNode类 * @return bool布尔型 */ pub fn isSameTree(&self, root1: Option<Box<TreeNode>>, root2: Option<Box<TreeNode>>) -> bool { if root1.is_none() && root2.is_none() { return true } if root1.is_none() || root2.is_none() { return false } return root1.as_ref().unwrap().val == root2.as_ref().unwrap().val && Solution::isSameTree(self, root1.as_ref().unwrap().left.clone(), root2.as_ref().unwrap().left.clone()) && Solution::isSameTree(self, root1.as_ref().unwrap().right.clone(), root2.as_ref().unwrap().right.clone()); } }