File tree 1 file changed +1
-34
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +1
-34
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .fishercoder .common .classes .TreeNode ;
4
4
5
- /**
6
- * 687. Longest Univalue Path
7
- *
8
- * Given a binary tree, find the length of the longest path where each node in the path has the same value.
9
- * This path may or may not pass through the root.
10
-
11
- Note: The length of path between two nodes is represented by the number of edges between them.
12
-
13
- Example 1:
14
- Input:
15
- 5
16
- / \
17
- 4 5
18
- / \ \
19
- 1 1 5
20
-
21
- Output:
22
- 2
23
-
24
- Example 2:
25
- Input:
26
-
27
- 1
28
- / \
29
- 4 5
30
- / \ \
31
- 4 4 5
32
- Output:
33
- 2
34
-
35
- Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.
36
- */
37
-
38
5
public class _687 {
39
6
public static class Solution1 {
40
7
/**
@@ -47,7 +14,7 @@ public int longestUnivaluePath(TreeNode root) {
47
14
}
48
15
return result [0 ];
49
16
}
50
-
17
+
51
18
// calculate longest univalue path from root to leaves
52
19
// In addition, the maximum univalue path cross the root node is calculated and then global maximum is udpated.
53
20
private int dfs (TreeNode root , int [] result ) {
You can’t perform that action at this time.
0 commit comments