File tree 2 files changed +43
-1
lines changed
2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,217 LeetCode solutions in JavaScript
1
+ # 1,218 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
985
985
1299|[ Replace Elements with Greatest Element on Right Side] ( ./solutions/1299-replace-elements-with-greatest-element-on-right-side.js ) |Easy|
986
986
1300|[ Sum of Mutated Array Closest to Target] ( ./solutions/1300-sum-of-mutated-array-closest-to-target.js ) |Medium|
987
987
1301|[ Number of Paths with Max Score] ( ./solutions/1301-number-of-paths-with-max-score.js ) |Hard|
988
+ 1302|[ Deepest Leaves Sum] ( ./solutions/1302-deepest-leaves-sum.js ) |Medium|
988
989
1304|[ Find N Unique Integers Sum up to Zero] ( ./solutions/1304-find-n-unique-integers-sum-up-to-zero.js ) |Easy|
989
990
1309|[ Decrypt String from Alphabet to Integer Mapping] ( ./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js ) |Easy|
990
991
1313|[ Decompress Run-Length Encoded List] ( ./solutions/1313-decompress-run-length-encoded-list.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1302. Deepest Leaves Sum
3
+ * https://leetcode.com/problems/deepest-leaves-sum/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given the root of a binary tree, return the sum of values of its deepest leaves.
7
+ */
8
+
9
+ /**
10
+ * Definition for a binary tree node.
11
+ * function TreeNode(val, left, right) {
12
+ * this.val = (val===undefined ? 0 : val)
13
+ * this.left = (left===undefined ? null : left)
14
+ * this.right = (right===undefined ? null : right)
15
+ * }
16
+ */
17
+ /**
18
+ * @param {TreeNode } root
19
+ * @return {number }
20
+ */
21
+ var deepestLeavesSum = function ( root ) {
22
+ let maxDepth = 0 ;
23
+ let sumAtMaxDepth = 0 ;
24
+
25
+ function traverse ( node , depth ) {
26
+ if ( ! node ) return ;
27
+
28
+ if ( depth > maxDepth ) {
29
+ maxDepth = depth ;
30
+ sumAtMaxDepth = node . val ;
31
+ } else if ( depth === maxDepth ) {
32
+ sumAtMaxDepth += node . val ;
33
+ }
34
+
35
+ traverse ( node . left , depth + 1 ) ;
36
+ traverse ( node . right , depth + 1 ) ;
37
+ }
38
+
39
+ traverse ( root , 0 ) ;
40
+ return sumAtMaxDepth ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments