-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongestTreeSumPathFromRootToLeaf.java
55 lines (52 loc) · 1.53 KB
/
LongestTreeSumPathFromRootToLeaf.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
47
48
49
50
51
52
53
54
55
/*https://binarysearch.com/problems/Longest-Tree-Sum-Path-From-Root-to-Leaf*/
import java.util.*;
// two dfs
// class Solution {
// int result, maxLevel;
// public int solve(Tree root) {
// result = Integer.MIN_VALUE;
// maxLevel = height(root);
// findSum(root,1,0);
// return result;
// }
// public void findSum(Tree root, int level, int sum)
// {
// if (root == null) return;
// findSum(root.left, level+1, sum+root.val);
// findSum(root.right, level+1, sum+root.val);
// if (level == maxLevel)
// {
// result = Math.max(sum+root.val, result);
// }
// }
// public int height(Tree root)
// {
// if (root == null) return 0;
// int left = height(root.left);
// int right = height(root.right);
// return Math.max(left,right)+1;
// }
// }
//single dfs
class Solution {
int result, maxLevel;
public int solve(Tree root) {
result = Integer.MIN_VALUE;
maxLevel = findSum(root,1,0);
return result;
}
public int findSum(Tree root, int level, int sum)
{
if (root == null) return 0;
int left = findSum(root.left, level+1, sum+root.val);
int right = findSum(root.right, level+1, sum+root.val);
if (level > maxLevel)
{
maxLevel = level;
result = sum+root.val;
}
else if (level == maxLevel)
result = Math.max(sum+root.val, result);
return Math.max(left, right)+1;
}
}