FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_13/LeetCode_687_13.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_13
/
LeetCode_687_13.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (40 loc) · 1.08 KB
Breadcrumbs
algorithm
/
Week_01
/
id_13
/
LeetCode_687_13.java
Copy path
File metadata and controls
46 lines (40 loc) · 1.08 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
43
44
45
46
package
leetCode
;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public
class
LeetCode_687_13
{
private
int
pathInt
;
public
int
longestUnivaluePath
(
TreeNode
root
) {
pathInt
=
0
;
_pathLength
(
root
);
return
pathInt
;
}
public
int
_pathLength
(
TreeNode
node
) {
if
(
node
==
null
)
return
0
;
int
left
=
_pathLength
(
node
.
left
);
int
right
=
_pathLength
(
node
.
right
);
int
pathLeft
=
0
,
pathRight
=
0
;
if
(
node
.
left
!=
null
&&
node
.
left
.
val
==
node
.
val
) {
pathLeft
+=
left
+
1
;
}
if
(
node
.
right
!=
null
&&
node
.
right
.
val
==
node
.
val
) {
pathRight
+=
right
+
1
;
}
pathInt
=
Math
.
max
(
pathInt
,
pathLeft
+
pathRight
);
return
Math
.
max
(
pathLeft
,
pathRight
);
}
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
) {
val
=
x
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL