-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongestPathConsecutiveSumBinaryTree.java
57 lines (47 loc) · 1.51 KB
/
longestPathConsecutiveSumBinaryTree.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
56
57
/*
Given a binary tree, find the length of the longest consecutive sequence path.
The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections.
*/class TreeVal {
int val;
int len=0;
TreeVal(int v, int l){
val=v;
len=l;
}
}
public int len = Integer.MIN_VALUE;
public int longestPathBinaryTree(TreeNode a)
{
if(a==null )
return 0;
if(a.left==null && a.right==null)
return a.val;
longestPathBinaryTreeHelper(a);
return len;
}
public TreeVal longestPathBinaryTreeHelper( TreeNode a) {
if(a==null)
return null;
if(a.left== null && a.right ==null)
{
return new TreeVal(a.val, 1);
}
TreeVal left = longestPathBinaryTreeHelper(a.left);
TreeVal right = longestPathBinaryTreeHelper(a.right);
if(left.val+1==a.val && a.val+1==right.val)
{
len = len<(left.len +right.len+1) ?left.len +right.len+1 : len;
return new TreeVal(a.val, Math.max(left.len+1, right.len+1));
}
else if(left.val+1==a.val)
{
len = len<(left.len+1) ?left.len+1 : len;
return new TreeVal(a.val, left.len+1);
}
else if(a.val+1==right.val)
{
len = len<(right.len+1) ?right.len+1 : len;
return new TreeVal(a.val, right.len+1);
}
return new TreeVal(a.val, 1);
}