File tree 1 file changed +48
-0
lines changed
1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ int sum = 0;
3
+ public int deepestLeavesSum(TreeNode root) {
4
+ int maxDepth = maxDepth(root);
5
+ findSum(root, 1, maxDepth);
6
+ return sum;
7
+ }
8
+
9
+ public int maxDepth(TreeNode node) {
10
+ if(node == null) return 0;
11
+ return 1 + Math.max(maxDepth(node.left), maxDepth(node.right));
12
+ }
13
+
14
+ public void findSum(TreeNode node, int curr, int depth) {
15
+ if(node != null) {
16
+ if(curr == depth) {
17
+ sum+=node.val;
18
+ }
19
+ findSum(node.left, curr+1, depth);
20
+ findSum(node.right, curr+1, depth);
21
+ }
22
+ }
23
+ }
24
+
25
+
26
+ class Solution {
27
+ int sum = 0;
28
+ int maxDepth = 0;
29
+ public int deepestLeavesSum(TreeNode root) {
30
+ findSum(root, 1);
31
+ return sum;
32
+ }
33
+
34
+
35
+ public void findSum(TreeNode node, int curr) {
36
+ if(node != null) {
37
+ if(curr > maxDepth) {
38
+ sum = 0;
39
+ maxDepth = curr;
40
+ }
41
+ if(curr == maxDepth) {
42
+ sum+=node.val;
43
+ }
44
+ findSum(node.left, curr+1);
45
+ findSum(node.right, curr+1);
46
+ }
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments