Skip to content

Commit f6bc1d8

Browse files
committed
Add solution #1302
1 parent 255f6e5 commit f6bc1d8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,217 LeetCode solutions in JavaScript
1+
# 1,218 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -985,6 +985,7 @@
985985
1299|[Replace Elements with Greatest Element on Right Side](./solutions/1299-replace-elements-with-greatest-element-on-right-side.js)|Easy|
986986
1300|[Sum of Mutated Array Closest to Target](./solutions/1300-sum-of-mutated-array-closest-to-target.js)|Medium|
987987
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|
988989
1304|[Find N Unique Integers Sum up to Zero](./solutions/1304-find-n-unique-integers-sum-up-to-zero.js)|Easy|
989990
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
990991
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|

solutions/1302-deepest-leaves-sum.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
};

0 commit comments

Comments
 (0)