We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a4722fe commit 891b970Copy full SHA for 891b970
src/com/gatsby/_1302DeepestLeavesSum.java
@@ -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