FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_24/leetcode_687_024.rb 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_24
/
leetcode_687_024.rb
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (28 loc) · 827 Bytes
Breadcrumbs
algorithm
/
Week_01
/
id_24
/
leetcode_687_024.rb
Copy path
File metadata and controls
33 lines (28 loc) · 827 Bytes
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
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Integer}
def
longest_univalue_path
(
root
)
path_length
=
0
get_path_length
(
root
)
return
path_length
end
def
get_path_length
(
root
)
return
0
if
root
.
nil?
left_path_length
=
get_path_length
(
root
.
left
)
right_path_length
=
get_path_length
(
root
.
right
)
left_length
,
right_length
=
0
,
0
if
!
root
.
left
.
nil?
&&
root
.
left
.
val
==
root
.
val
left_length
=
left_path_length
+
1
elsif
!
root
.
right
.
nil?
&&
root
.
right
.
val
==
root
.
val
right_length
=
right_path_length
+
1
end
path_length
=
[
path_length
,
(
left_length
+
right_length
)
]
.
max
return
[
left_length
,
right_length
]
.
max
end
Back
|
FazBrowse Home
|
New Git URL