Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
bc28aa94
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,发现更多精彩内容 >>
提交
bc28aa94
编写于
7月 13, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add scala #117*
上级
3c658259
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
86 addition
and
0 deletion
+86
-0
scala-leetcode/README.md
scala-leetcode/README.md
+1
-0
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_117.scala
...de/src/main/scala/io/github/dreamylost/Leetcode_117.scala
+85
-0
未找到文件。
scala-leetcode/README.md
浏览文件 @
bc28aa94
...
...
@@ -63,6 +63,7 @@ Leetcode Scala 实现
*
[
最大二叉树 II
](
./src/main/scala/io/github/dreamylost/Leetcode_998.scala
)
*
[
先序遍历构造二叉树
](
./src/main/scala/io/github/dreamylost/Leetcode_1008.scala
)
*
[
填充每个节点的下一个右侧节点指针
](
./src/main/scala/io/github/dreamylost/Leetcode_116.scala
)
*
[
填充每个节点的下一个右侧节点指针 II
](
./src/main/scala/io/github/dreamylost/Leetcode_117.scala
)
## 链表
...
...
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_117.scala
0 → 100644
浏览文件 @
bc28aa94
/* Licensed under Apache-2.0 @梦境迷离 */
package
io.github.dreamylost
/**
* 117. 填充每个节点的下一个右侧节点指针 II
*
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
*
* 初始状态下,所有 next 指针都被设置为 NULL。
*
* @author 梦境迷离 dreamylost
* @since 2020-07-13
* @version v1.0
*/
object
Leetcode_117
extends
App
{
val
ret
=
connect
(
Node
.
getNode_7
())
println
(
ret
)
/**
* 层序遍历链接每一层
*
* 递归与116类似,区别:不是完全二叉树
*
* 636 ms,20.00%
* 52.9 MB,100.00%
*
* @param root
* @return
*/
def
connect
(
root
:
Node
)
:
Node
=
{
if
(
root
==
null
)
return
null
var
queue
=
Seq
[
Node
]()
queue
=
queue
++
Seq
(
root
)
while
(
queue
.
nonEmpty
)
{
val
size
=
queue
.
size
for
(
i
<-
0
until
size
)
{
val
node
=
queue
.
head
queue
=
queue
.
tail
//确保只在当前层级结束时才建立下一个指针
if
(
i
<
size
-
1
)
{
node
.
next
=
queue
.
head
}
if
(
node
.
left
!=
null
)
queue
=
queue
++
Seq
(
node
.
left
)
if
(
node
.
right
!=
null
)
queue
=
queue
++
Seq
(
node
.
right
)
}
}
root
}
/**
* 不使用队列,把每层都看成列表
*
* 556 ms,80.00%
* 53.2 MB,100.00%
*
* @param root
* @return
*/
def
connect2
(
root
:
Node
)
=
{
var
head
:
Node
=
root
while
(
head
!=
null
)
{
//链表头
val
level
=
new
Node
(
Int
.
MinValue
)
var
tail
=
level
while
(
head
!=
null
)
{
if
(
head
.
left
!=
null
)
{
tail
.
next
=
head
.
left
tail
=
tail
.
next
}
if
(
head
.
right
!=
null
)
{
tail
.
next
=
head
.
right
tail
=
tail
.
next
}
head
=
head
.
next
}
//每次head都从链表头开始
head
=
level
.
next
}
root
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录