题解 | #对称的二叉树# | Rust
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/** * #[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{} } fn dfs(&self, one: Option<Box<TreeNode>>, two: Option<Box<TreeNode>>) -> bool { if one.is_none() && two.is_none() { return true; } if one.is_none() || two.is_none() { return false; } return one.as_ref().unwrap().val == two.as_ref().unwrap().val && Solution::dfs(self, one.as_ref().unwrap().left.clone(), two.as_ref().unwrap().right.clone()) && Solution::dfs(self, one.as_ref().unwrap().right.clone(), two.as_ref().unwrap().left.clone()); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pRoot TreeNode类 * @return bool布尔型 */ pub fn isSymmetrical(&self, pRoot: Option<Box<TreeNode>>) -> bool { return Solution::dfs(self, pRoot.clone(), pRoot.clone()); } }