FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_118/leetcode_687_118.java at master · cloudrib/algorithm · GitHub
cloudrib
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_687_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (34 loc) · 1 KB
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_687_118.java
Copy path
File metadata and controls
42 lines (34 loc) · 1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class
Solution
{
int
ans
;
public
int
longestUnivaluePath
(
TreeNode
root
) {
ans
=
0
;
longestUnivaluePathByRoot
(
root
);
return
ans
;
}
// 从 root 向下的最长同值路径。
public
int
longestUnivaluePathByRoot
(
TreeNode
root
){
if
(
null
==
root
){
return
0
;
}
int
left
=
longestUnivaluePathByRoot
(
root
.
left
);
int
right
=
longestUnivaluePathByRoot
(
root
.
right
);
int
arrowLeft
=
0
,
arrowRight
=
0
;
if
(
root
.
left
!=
null
&&
root
.
left
.
val
==
root
.
val
){
arrowLeft
+=
left
+
1
;
}
if
(
root
.
right
!=
null
&&
root
.
right
.
val
==
root
.
val
){
arrowRight
+=
right
+
1
;
}
ans
=
Math
.
max
(
ans
,
arrowLeft
+
arrowRight
);
return
Math
.
max
(
arrowLeft
,
arrowRight
);
}
}
Back
|
FazBrowse Home
|
New Git URL