-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1302.Deepest-Leaves-Sum.java
46 lines (35 loc) · 1.03 KB
/
1302.Deepest-Leaves-Sum.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
43
44
45
46
// https://leetcode.com/problems/deepest-leaves-sum/
// algorithms
// Medium (86.67%)
// Total Accepted: 4,080
// Total Submissions: 4,708
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
class Solution {
private static int res = 0;
public int deepestLeavesSum(TreeNode root) {
res = 0;
int height = getHeight(root) - 1;
recursive(root, height, 0);
return res;
}
public int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
public void recursive(TreeNode root, int targetHeight, int curHeight) {
if (root == null) {
return;
}
if (curHeight == targetHeight) {
res += root.val;
return;
}
recursive(root.left, targetHeight, curHeight + 1);
recursive(root.right, targetHeight, curHeight + 1);
}
}