Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
c6e91bbe
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 搜索 >>
提交
c6e91bbe
编写于
3月 18, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor code
上级
c04b223d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
39 deletion
+36
-39
leetcode/src/main/scala/io/github/dreamylost/DFS.scala
leetcode/src/main/scala/io/github/dreamylost/DFS.scala
+36
-39
未找到文件。
leetcode/src/main/scala/io/github/dreamylost/DFS.scala
浏览文件 @
c6e91bbe
package
io.github.dreamylost
import
scala.collection.mutable.
{
Queue
,
Seq
,
Stack
}
//隐藏java集合
import
java.util.
{
Queue
=>
_
}
import
scala.collection.mutable
/**
* 树的遍历
*/
* 树的遍历
*/
object
DFS
extends
App
{
//前
...
...
@@ -32,32 +36,29 @@ object DFS extends App {
println
(
root
.
value
)
}
import
util.control.Breaks._
//前,144. Binary Tree Preorder Traversal (Medium)
def
preorderTraversal
(
root
:
TreeNode
)
:
Seq
[
Int
]
=
{
val
ret
=
Seq
[
Int
]()
val
stack
=
Stack
[
TreeNode
]()
def
preorderTraversal
(
root
:
TreeNode
)
:
mutable.
Seq
[
Int
]
=
{
val
ret
=
mutable
.
Seq
[
Int
]()
val
stack
=
mutable
.
Stack
[
TreeNode
]()
stack
.
push
(
root
)
while
(
!
stack
.
is
Empty
)
{
while
(
stack
.
non
Empty
)
{
val
node
=
stack
.
pop
()
breakable
{
if
(
node
==
null
)
break
if
(
node
!=
null
)
{
ret
.:+(
node
.
value
)
stack
.
push
(
node
.
right
)
// 先右后左,保证左子树先遍历
stack
.
push
(
node
.
left
)
}
ret
.:+(
node
.
value
)
stack
.
push
(
node
.
right
)
// 先右后左,保证左子树先遍历
stack
.
push
(
node
.
left
)
}
ret
urn
ret
ret
}
//中, 94. Binary Tree Inorder Traversal (Medium)
def
inorderTraversal
(
root
:
TreeNode
)
:
Seq
[
Int
]
=
{
val
ret
=
Seq
[
Int
]()
val
stack
=
Stack
[
TreeNode
]()
def
inorderTraversal
(
root
:
TreeNode
)
:
mutable.
Seq
[
Int
]
=
{
val
ret
=
mutable
.
Seq
[
Int
]()
val
stack
=
mutable
.
Stack
[
TreeNode
]()
if
(
root
==
null
)
return
ret
var
cur
=
root
while
(
cur
!=
null
||
!
stack
.
is
Empty
)
{
while
(
cur
!=
null
||
stack
.
non
Empty
)
{
while
(
cur
!=
null
)
{
stack
.
push
(
cur
)
cur
=
cur
.
left
...
...
@@ -70,38 +71,34 @@ object DFS extends App {
}
/**
* [因为是栈,先左子树出栈,后右子树出栈]
* 前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。
*/
* [因为是栈,先左子树出栈,后右子树出栈]
* 前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。
*/
//后,145. Binary Tree Postorder Traversal (Medium)
def
postorderTraversal
(
root
:
TreeNode
)
:
Seq
[
Int
]
=
{
val
ret
=
Seq
[
Int
]()
val
stack
=
Stack
[
TreeNode
]()
def
postorderTraversal
(
root
:
TreeNode
)
:
mutable.
Seq
[
Int
]
=
{
val
ret
=
mutable
.
Seq
[
Int
]()
val
stack
=
mutable
.
Stack
[
TreeNode
]()
stack
.
push
(
root
)
while
(
!
stack
.
is
Empty
)
{
while
(
stack
.
non
Empty
)
{
val
node
=
stack
.
pop
()
breakable
{
if
(
node
==
null
)
break
if
(
node
!=
null
)
{
ret
.:+(
node
.
value
)
stack
.
push
(
node
.
left
)
// 先右后左,保证左子树先遍历
stack
.
push
(
node
.
right
)
}
ret
.:+(
node
.
value
)
stack
.
push
(
node
.
left
)
// 先右后左,保证左子树先遍历
stack
.
push
(
node
.
right
)
}
ret
.
reverse
}
//隐藏java集合
import
java.util.
{
Queue
=>
_
}
//层序
def
levelTraverse
(
root
:
TreeNode
)
:
Seq
[
Int
]
=
{
if
(
root
==
null
)
return
Seq
()
val
list
=
Seq
[
Int
]();
def
levelTraverse
(
root
:
TreeNode
)
:
mutable.
Seq
[
Int
]
=
{
if
(
root
==
null
)
return
mutable
.
Seq
()
val
list
=
mutable
.
Seq
[
Int
]();
//Scala的Seq将是Java的List,Scala的List将是Java的LinkedList。
val
queue
=
new
Queue
[
TreeNode
]()
//层序遍历时保存结点的队列,可以省略new或者省略()
val
queue
=
mutable
.
Queue
[
TreeNode
]()
//层序遍历时保存结点的队列,可以省略new或者省略()
queue
.
enqueue
(
root
)
//初始化
while
(
!
queue
.
is
Empty
)
{
while
(
queue
.
non
Empty
)
{
val
node
=
queue
.
dequeue
list
.:+(
node
.
value
)
if
(
node
.
left
!=
null
)
queue
.
enqueue
(
node
.
left
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录