Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
66a6499d
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
11 个月 前同步成功
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cs Summary Reflection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
66a6499d
编写于
6月 26, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
java leetcode jz 68-2
上级
1110e91c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
116 addition
and
0 deletion
+116
-0
java-leetcode/README.md
java-leetcode/README.md
+1
-0
java-leetcode/src/main/java/io/github/dreamylost/Leetcode_JZ_68_2.java
.../src/main/java/io/github/dreamylost/Leetcode_JZ_68_2.java
+115
-0
未找到文件。
java-leetcode/README.md
浏览文件 @
66a6499d
...
@@ -10,3 +10,4 @@
...
@@ -10,3 +10,4 @@
*
[
输出二叉树中所有从根到叶子的路径
](
./src/main/java/io/github/dreamylost/Leetcode_257_DFS.java
)
*
[
输出二叉树中所有从根到叶子的路径
](
./src/main/java/io/github/dreamylost/Leetcode_257_DFS.java
)
*
[
BiNode
](
./src/main/java/io/github/dreamylost/Leetcode_Interview_17_12.java
)
*
[
BiNode
](
./src/main/java/io/github/dreamylost/Leetcode_Interview_17_12.java
)
*
[
二叉搜索树的最近公共祖先
](
./src/main/java/io/github/dreamylost/Leetcode_JZ_68_1.java
)
*
[
二叉搜索树的最近公共祖先
](
./src/main/java/io/github/dreamylost/Leetcode_JZ_68_1.java
)
*
[
二叉树的最近公共祖先
](
./src/main/java/io/github/dreamylost/Leetcode_JZ_68_2.java
)
java-leetcode/src/main/java/io/github/dreamylost/Leetcode_JZ_68_2.java
0 → 100644
浏览文件 @
66a6499d
/* Licensed under Apache-2.0 @梦境迷离 */
package
io.github.dreamylost
;
import
java.util.*
;
/**
* 剑指 Offer 68 - II. 二叉树的最近公共祖先
*
* @author 梦境迷离 dreamylost
* @version v1.0
* @since 2020-06-26
*/
public
class
Leetcode_JZ_68_2
{
/**
* 7 ms,100.00% 41.8 MB,100.00%
*
* @param root
* @param p
* @param q
* @return
*/
public
TreeNode
lowestCommonAncestor
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
)
{
//当p或q本身就是根节点时,公共就是p或q本身,因为根之上没有父节点
if
(
root
==
null
||
root
==
p
||
root
==
q
)
{
return
root
;
}
TreeNode
left
=
lowestCommonAncestor
(
root
.
left
,
p
,
q
);
TreeNode
right
=
lowestCommonAncestor
(
root
.
right
,
p
,
q
);
if
(
left
==
null
&&
right
==
null
)
{
return
null
;
}
if
(
left
!=
null
&&
right
!=
null
)
{
return
root
;
}
return
left
==
null
?
right
:
left
;
}
/**
* author 2hp 暴力 7 ms,100.00% 40.9 MB,100.00%
*
* @param root
* @param p
* @param q
* @return
*/
public
TreeNode
lowestCommonAncestor2
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
)
{
//如果从p开始向下找有q则返回p
if
(
dfs
(
p
,
q
))
return
p
;
//如果从q开始向下找有p返回q
if
(
dfs
(
q
,
p
))
return
q
;
//否则
return
help
(
root
,
p
,
q
);
}
//方法:在root向下寻找是否有search节点
public
boolean
dfs
(
TreeNode
root
,
TreeNode
search
)
{
if
(
root
==
null
)
return
false
;
if
(
root
.
val
==
search
.
val
)
return
true
;
else
return
dfs
(
root
.
left
,
search
)
||
dfs
(
root
.
right
,
search
);
}
//方法:1.root子节点中分别含有p、q
//2.root单边含有p、q
public
TreeNode
help
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
)
{
//如果root.left下有p并且root.right下有q返回root;相反也是一样返回root
if
((
dfs
(
root
.
left
,
p
)
&&
dfs
(
root
.
right
,
q
))
||
dfs
(
root
.
left
,
q
)
&&
dfs
(
root
.
right
,
p
))
{
return
root
;
}
else
if
(
dfs
(
root
.
left
,
p
)
&&
dfs
(
root
.
left
,
q
))
return
help
(
root
.
left
,
p
,
q
);
//否则p,q就是在同一边,递归调用即可
else
return
help
(
root
.
right
,
p
,
q
);
}
/**
* author sdwwld
*
* <p>15 ms,7.16% 40 MB,100.00%
*
* <p>非递归 BFS
*
* @param root
* @param p
* @param q
* @return
*/
public
TreeNode
lowestCommonAncestor3
(
TreeNode
root
,
TreeNode
p
,
TreeNode
q
)
{
//记录遍历到的每个节点的父节点
Map
<
TreeNode
,
TreeNode
>
parentMap
=
new
HashMap
<>();
Queue
<
TreeNode
>
queue
=
new
LinkedList
<>();
parentMap
.
put
(
root
,
null
);
queue
.
add
(
root
);
//直到两个节点都找到为止
while
(!
parentMap
.
containsKey
(
p
)
||
!
parentMap
.
containsKey
(
q
))
{
TreeNode
node
=
queue
.
poll
();
if
(
node
!=
null
&&
node
.
left
!=
null
)
{
parentMap
.
put
(
node
.
left
,
node
);
queue
.
add
(
node
.
left
);
}
if
(
node
!=
null
&&
node
.
right
!=
null
)
{
parentMap
.
put
(
node
.
right
,
node
);
queue
.
add
(
node
.
right
);
}
}
Set
<
TreeNode
>
ancestorsSet
=
new
HashSet
<>();
//记录下p和他的祖先节点,从p节点开始一直到根节点
while
(
p
!=
null
)
{
ancestorsSet
.
add
(
p
);
p
=
parentMap
.
get
(
p
);
}
//查看p和他的祖先节点是否包含q节点,如果不包含再看是否包含q的父节点
while
(!
ancestorsSet
.
contains
(
q
))
q
=
parentMap
.
get
(
q
);
return
q
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录