/** * #[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 root TreeNode类 * @return int整型 */ pub fn sumOfLeftLeaves(&self, root: Option<Box<TreeNode>>) -> i32 { if root.is_none() { return 0; } let mut ans : i32 = 0; if root.as_ref().unwrap().left.is_some() { let l = root.as_ref().unwrap().left.as_ref(); if l.unwrap().left.is_none() && l.unwrap().right.is_none() { ans += l.unwrap().val; } } return ans + Solution::sumOfLeftLeaves(self, root.as_ref().unwrap().left.clone()) + Solution::sumOfLeftLeaves(self, root.as_ref().unwrap().right.clone()); } }