/* * @lc app=leetcode id=1302 lang=rust * * [1302] Deepest Leaves Sum * * https://leetcode.com/problems/deepest-leaves-sum/description/ * * algorithms * Medium (83.19%) * Total Accepted: 23K * Total Submissions: 27.6K * Testcase Example: '[1,2,3,4,5,null,6,7,null,null,null,null,8]' * * Given a binary tree, return the sum of values of its deepest leaves. * * Example 1: * * * * * Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8] * Output: 15 * * * * Constraints: * * * The number of nodes in the tree is between 1 and 10^4. * The value of nodes is between 1 and 100. * */ // Definition for a binary tree node. // #[derive(Debug, PartialEq, Eq)] // // pub structTreeNode { // pub val: i32, // pub left: Option>>, // pub right: Option>>, // } // 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 bfs(root: Option>>, maxdepth: usize, aux: &mut Vec) -> i32{ if let Some(root) = root { aux[maxdepth] += root.borrow().val; Solution::bfs(root.borrow().left.clone(), maxdepth + 1, aux); Solution::bfs(root.borrow().right.clone(), maxdepth + 1, aux); } 0 } pub fn deepest_leaves_sum(root: Option>>) -> i32 { let mut maxdepth = 0_usize; let mut aux = vec![0; 10001]; Solution::bfs(root, 0, &mut aux); let mut res = 0; for n in aux { if n > 0 { res = n ;} } res } } // pub structSolution; use std::collections::HashMap; use std::collections::HashSet; use std::fmt::Debug; use std::hash::Hash; use std::iter::FromIterator; // use std::collections::VecDeque; // use std::collections::BTreeMap; #[allow(dead_code)] pub fn print_map(map: &HashMap) { for (k, v) in map.iter() { println!("{:?}: {:?}", k, v); } } #[allow(dead_code)] pub fn say_vec(nums: Vec){ println!("{:?}", nums); } #[allow(dead_code)] pub fn char_frequency(s: String) -> HashMap { let mut res:HashMap = HashMap::new(); for c in s.chars(){ *res.entry(c).or_insert(0) += 1; } res } #[allow(dead_code)] pub fn vec_counter(arr: Vec) -> HashMap { let mut c = HashMap::new(); for n in arr { *c.entry(n).or_insert(0) += 1; } c } #[allow(dead_code)] pub fn vec_to_hashset(arr: Vec) -> HashSet { HashSet::from_iter(arr.iter().cloned()) } #[allow(dead_code)] pub fn int_to_char(n: i32) -> char { // Convert number 0 to a, 1 to b, ... assert!(n >= 0 && n <= 25); (n as u8 + 'a' as u8) as char } #[allow(dead_code)] fn sayi32(i: i32) { println!("{}", i); } #[allow(dead_code)] fn sayi32_arr(arr: &Vec) { println!("{:?}", arr); } #[allow(dead_code)] pub fn bisect_left(arr: &Vec, target: i32) -> usize { let (mut lo, mut hi) = (0, arr.len() - 1); let mut mid; while lo < hi { mid = (lo + hi) >> 1; if arr[mid as usize] >= target { hi = mid; } else { lo = mid + 1; } } lo } #[allow(dead_code)] pub fn bisect_right(arr: &Vec, target: i32) -> usize { let (mut lo, mut hi) = (0, arr.len() - 1); let mut mid; while lo < hi { mid = (lo + hi + 1) >> 1; if arr[mid as usize] > target { hi = mid - 1; } else { lo = mid; } } if arr[hi] > target { hi } else {hi + 1} }