File tree 2 files changed +43
-0
lines changed
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 348
348
1103|[ Distribute Candies to People] ( ./1103-distribute-candies-to-people.js ) |Easy|
349
349
1108|[ Defanging an IP Address] ( ./1108-defanging-an-ip-address.js ) |Easy|
350
350
1122|[ Relative Sort Array] ( ./1122-relative-sort-array.js ) |Easy|
351
+ 1161|[ Maximum Level Sum of a Binary Tree] ( ./1161-maximum-level-sum-of-a-binary-tree.js ) |Medium|
351
352
1189|[ Maximum Number of Balloons] ( ./1189-maximum-number-of-balloons.js ) |Easy|
352
353
1200|[ Minimum Absolute Difference] ( ./1200-minimum-absolute-difference.js ) |Easy|
353
354
1206|[ Design Skiplist] ( ./1206-design-skiplist.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1161. Maximum Level Sum of a Binary Tree
3
+ * https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given the root of a binary tree, the level of its root is 1, the level of its
7
+ * children is 2, and so on.
8
+ *
9
+ * Return the smallest level x such that the sum of all the values of nodes at
10
+ * level x is maximal.
11
+ */
12
+
13
+ /**
14
+ * Definition for a binary tree node.
15
+ * function TreeNode(val, left, right) {
16
+ * this.val = (val===undefined ? 0 : val)
17
+ * this.left = (left===undefined ? null : left)
18
+ * this.right = (right===undefined ? null : right)
19
+ * }
20
+ */
21
+ /**
22
+ * @param {TreeNode } root
23
+ * @return {number }
24
+ */
25
+ var maxLevelSum = function ( root ) {
26
+ const sums = [ - Infinity ] ;
27
+
28
+ traverse ( root , 1 ) ;
29
+
30
+ return sums . indexOf ( Math . max ( ...sums ) ) ;
31
+
32
+ function traverse ( node , depth ) {
33
+ if ( ! node ) return ;
34
+ if ( sums [ depth ] === undefined ) {
35
+ sums . push ( node . val ) ;
36
+ } else {
37
+ sums [ depth ] += node . val ;
38
+ }
39
+ traverse ( node . left , depth + 1 ) ;
40
+ traverse ( node . right , depth + 1 ) ;
41
+ }
42
+ } ;
You can’t perform that action at this time.
0 commit comments