FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_113/leetcode_236_113.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
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_02
/
id_113
/
leetcode_236_113.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
46 lines (41 loc) · 1.29 KB
Breadcrumbs
algorithm
/
Week_02
/
id_113
/
leetcode_236_113.java
Copy path
File metadata and controls
46 lines (41 loc) · 1.29 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class
Solution
{
public
TreeNode
lowestCommonAncestor
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
) {
ArrayList
<
TreeNode
>
routes1
=
new
ArrayList
();
ArrayList
<
TreeNode
>
routes2
=
new
ArrayList
();
FindRoute
(
root
,
p
.
val
,
routes1
);
FindRoute
(
root
,
q
.
val
,
routes2
);
return
Lca
(
routes1
,
routes2
);
}
public
boolean
FindRoute
(
TreeNode
root
,
Integer
p
,
List
<
TreeNode
>
routes
) {
if
(
root
==
null
)
return
false
;
routes
.
add
(
root
);
if
(
root
.
val
==
p
)
return
true
;
if
(
FindRoute
(
root
.
left
,
p
,
routes
))
return
true
;
if
(
FindRoute
(
root
.
right
,
p
,
routes
))
return
true
;
routes
.
remove
(
root
);
return
false
;
}
public
TreeNode
Lca
(
ArrayList
<
TreeNode
>
nodes1
,
ArrayList
<
TreeNode
>
nodes2
) {
int
c
=
nodes1
.
size
() >
nodes2
.
size
() ?
nodes2
.
size
() :
nodes1
.
size
();
TreeNode
last
=
null
;
for
(
int
i
=
0
;
i
<
c
;
i
++) {
TreeNode
a
=
nodes1
.
get
(
i
);
TreeNode
b
=
nodes2
.
get
(
i
);
if
(
a
==
b
) {
last
=
a
;
}
else
{
break
;
}
}
return
last
;
}
}
Back
|
FazBrowse Home
|
New Git URL