1161. Maximum Level Sum of a Binary Tree Medium

@problem@discussion
#Tree#Depth-First Search#Breadth-First Search#Binary Tree



1/**
2 * [1161] Maximum Level Sum of a Binary Tree
3 *
4 * Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
5 * Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/capture.JPG" style="width: 200px; height: 175px;" />
9 * Input: root = [1,7,0,7,-8,null,null]
10 * Output: 2
11 * Explanation: 
12 * Level 1 sum = 1.
13 * Level 2 sum = 7 + 0 = 7.
14 * Level 3 sum = 7 + -8 = -1.
15 * So we return the level with the maximum sum which is level 2.
16 * 
17 * Example 2:
18 * 
19 * Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
20 * Output: 2
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	The number of nodes in the tree is in the range [1, 10^4].
26 * 	-10^5 <= Node.val <= 10^5
27 * 
28 */
29pub struct Solution {}
30use crate::util::tree::{TreeNode, to_tree};
31
32// problem: https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
33// discuss: https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37// Definition for a binary tree node.
38// #[derive(Debug, PartialEq, Eq)]
39// pub struct TreeNode {
40//   pub val: i32,
41//   pub left: Option<Rc<RefCell<TreeNode>>>,
42//   pub right: Option<Rc<RefCell<TreeNode>>>,
43// }
44// 
45// impl TreeNode {
46//   #[inline]
47//   pub fn new(val: i32) -> Self {
48//     TreeNode {
49//       val,
50//       left: None,
51//       right: None
52//     }
53//   }
54// }
55use std::rc::Rc;
56use std::cell::RefCell;
57impl Solution {
58    pub fn max_level_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
59        0
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_1161() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.