-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDeepestLeavesSum.java
42 lines (33 loc) · 1.04 KB
/
DeepestLeavesSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.TreeNode;
// https://leetcode.com/problems/deepest-leaves-sum/
public class DeepestLeavesSum {
private final TreeNode input;
private int maxDeep = 0;
public DeepestLeavesSum(TreeNode input) {
this.input = input;
}
public int solution() {
maxDeep = maxDeep(input, 0);
return traverse(input, 0);
}
private int maxDeep(TreeNode node, int curr) {
if (node != null) {
int left = maxDeep(node.left, curr + 1);
int right = maxDeep(node.right, curr + 1);
return Math.max(left, right);
}
return curr;
}
private int traverse(TreeNode node, int curr) {
if (node != null) {
if (curr + 1 == maxDeep) {
return node.val;
}
int left = traverse(node.left, curr + 1);
int right = traverse(node.right, curr + 1);
return left + right;
}
return 0;
}
}