Skip to content

Commit 4a4af4c

Browse files
committed
✨ deepest leaves sum
1 parent c22bca6 commit 4a4af4c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CATEGORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- 数组
22
- 字符串
33
- 哈希表
4+
- 排序
45

56
- 栈、队列
67
649
@@ -10,6 +11,10 @@
1011

1112
- 二分查找
1213

14+
- 二叉搜索树
15+
16+
- 深度优先搜索 Depth First Search
17+
1318
- 双指针
1419
881
1520

NOTE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
二叉搜索树的定义:对于树中的所有子树都有,左子树上的值都小于根节点的值,右子树上的值都大于根节点上的值

src/1302-deepest-leaves-sum/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
var deepestLeavesSum = function (root) {
13+
let res = [];
14+
dfs(root, 0);
15+
return res.slice(-1)[0];
16+
17+
function dfs(node, depth = 0) {
18+
if (node === null) return;
19+
if (node.left === null && node.right === null) {
20+
res[depth] = ~~res[depth] + node.val;
21+
return;
22+
}
23+
24+
dfs(node.left, depth + 1);
25+
dfs(node.right, depth + 1);
26+
}
27+
};

0 commit comments

Comments
 (0)