Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
5d3b48ab
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
10 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
5d3b48ab
编写于
4月 23, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
scala leetcode 559
上级
730d024f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
90 addition
and
0 deletion
+90
-0
scala-leetcode/README.md
scala-leetcode/README.md
+31
-0
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_559.scala
...de/src/main/scala/io/github/dreamylost/Leetcode_559.scala
+59
-0
未找到文件。
scala-leetcode/README.md
浏览文件 @
5d3b48ab
...
...
@@ -1022,4 +1022,35 @@
}
ret
}
```
*
N叉树的最大深度
```
scala
//DFS
def
maxDepth
(
root
:
Node
)
:
Int
=
{
if
(
root
==
null
)
return
0
var
depth
=
0
for
(
i
<-
root
.
children
.
indices
)
{
depth
=
Math
.
max
(
depth
,
maxDepth
(
root
.
children
(
i
)))
}
depth
+
1
}
//层序遍历
def
maxDepth2
(
root
:
Node
)
:
Int
=
{
if
(
root
==
null
)
return
0
if
(
root
.
children
.
isEmpty
)
return
1
var
depth
=
0
var
queue
=
root
::
List
()
while
(
queue
.
nonEmpty
)
{
depth
+=
1
queue
.
indices
foreach
{
_
=>
val
node
=
queue
.
take
(
1
).
headOption
queue
=
queue
.
drop
(
1
)
if
(
node
.
fold
(
false
)(
_
.
children
.
nonEmpty
))
{
queue
=
queue
:::
node
.
fold
(
List
[
Node
]())(
_
.
children
)
}
}
}
depth
}
```
\ No newline at end of file
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_559.scala
0 → 100644
浏览文件 @
5d3b48ab
package
io.github.dreamylost
/**
* 559. N叉树的最大深度
*
* @author 梦境迷离
* @since 2020-04-23
* @version v1.0
*/
object
Leetcode_559
extends
App
{
val
node5
=
new
Node
(
5
)
val
node6
=
new
Node
(
6
)
val
node3
=
new
Node
(
3
)
val
node2
=
new
Node
(
2
)
val
node4
=
new
Node
(
4
)
val
node1
=
new
Node
(
1
)
node3
.
children
=
List
(
node5
,
node6
)
node1
.
children
=
List
(
node3
,
node2
,
node4
)
println
(
maxDepth
(
node1
))
println
(
maxDepth2
(
node1
))
//N叉树
class
Node
(
var
_value
:
Int
)
{
var
value
:
Int
=
_value
var
children
:
List
[
Node
]
=
List
()
}
//DFS
def
maxDepth
(
root
:
Node
)
:
Int
=
{
if
(
root
==
null
)
return
0
var
depth
=
0
for
(
i
<-
root
.
children
.
indices
)
{
depth
=
Math
.
max
(
depth
,
maxDepth
(
root
.
children
(
i
)))
}
depth
+
1
}
//层序遍历
def
maxDepth2
(
root
:
Node
)
:
Int
=
{
if
(
root
==
null
)
return
0
if
(
root
.
children
.
isEmpty
)
return
1
var
depth
=
0
var
queue
=
root
::
List
()
while
(
queue
.
nonEmpty
)
{
depth
+=
1
queue
.
indices
foreach
{
_
=>
val
node
=
queue
.
take
(
1
).
headOption
queue
=
queue
.
drop
(
1
)
if
(
node
.
fold
(
false
)(
_
.
children
.
nonEmpty
))
{
queue
=
queue
:::
node
.
fold
(
List
[
Node
]())(
_
.
children
)
}
}
}
depth
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录