Skip to content

Commit 891b970

Browse files
committed
1302. 层数最深叶子节点的和
1 parent a4722fe commit 891b970

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.gatsby;
2+
3+
4+
5+
/**
6+
* @ClassName: _1302DeepestLeavesSum
7+
* @Description:
8+
* @author: Gatsby
9+
* @date: 2022/7/21 13:35
10+
*/
11+
12+
public class _1302DeepestLeavesSum {
13+
private int maxDepth = 0;
14+
private int sum;
15+
16+
private void dfs(TreeNode root, int depth) {
17+
if (root == null) {
18+
return;
19+
}
20+
++depth;
21+
if (depth > maxDepth) {
22+
maxDepth = depth;
23+
sum = root.val;
24+
} else if (depth == maxDepth) {
25+
sum += root.val;
26+
}
27+
dfs(root.left, depth);
28+
dfs(root.right, depth);
29+
}
30+
31+
public int deepestLeavesSum(TreeNode root) {
32+
dfs(root, 0);
33+
return sum;
34+
}
35+
}
36+
37+

0 commit comments

Comments
 (0)