Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
6bc81647
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,发现更多精彩内容 >>
提交
6bc81647
编写于
7月 12, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add scala #1008
上级
1d77d84f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
0 deletion
+46
-0
scala-leetcode/README.md
scala-leetcode/README.md
+1
-0
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1008.scala
...e/src/main/scala/io/github/dreamylost/Leetcode_1008.scala
+45
-0
未找到文件。
scala-leetcode/README.md
浏览文件 @
6bc81647
...
...
@@ -61,6 +61,7 @@ Leetcode Scala 实现
*
[
从叶结点开始的最小字符串
](
./src/main/scala/io/github/dreamylost/Leetcode_988.scala
)
*
[
按垂序遍历返回其结点值
](
./src/main/scala/io/github/dreamylost/Leetcode_987.scala
)
*
[
最大二叉树 II
](
./src/main/scala/io/github/dreamylost/Leetcode_998.scala
)
*
[
先序遍历构造二叉树
](
./src/main/scala/io/github/dreamylost/Leetcode_1008.scala
)
## 链表
...
...
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1008.scala
0 → 100644
浏览文件 @
6bc81647
/* Licensed under Apache-2.0 @梦境迷离 */
package
io.github.dreamylost
/**
* 1008. 先序遍历构造二叉树
*
* 返回与给定先序遍历 preorder 相匹配的二叉搜索树(binary search tree)的根结点。
*
* (回想一下,二叉搜索树是二叉树的一种,其每个节点都满足以下规则,对于 node.left 的任何后代,值总 < node.val,而 node.right 的任何后代,值总 > node.val。
* 此外,先序遍历首先显示节点的值,然后遍历 node.left,接着遍历 node.right。)
*
* @author 梦境迷离 dreamylost
* @since 2020-07-12
* @version v1.0
*/
object
Leetcode_1008
extends
App
{
val
ret
=
bstFromPreorder
(
Array
(
8
,
5
,
1
,
7
,
10
,
12
))
println
(
ret
)
/**
* 已知前序序列,找到第一个不满足左子树的节点,作用相当于在中序序列中构造二叉搜索树时计算的mid
* 548 ms,100.00%
* 51 MB,100.00%
*
* @param preorder
* @return
*/
def
bstFromPreorder
(
preorder
:
Array
[
Int
])
:
TreeNode
=
{
def
buildSearchTree
(
values
:
Array
[
Int
],
l
:
Int
,
r
:
Int
)
:
TreeNode
=
{
if
(
l
>
r
)
{
return
null
}
val
root
=
new
TreeNode
(
values
(
l
))
var
index
=
l
while
(
index
<=
r
&&
preorder
(
index
)
<=
preorder
(
l
))
index
+=
1
//head已经被使用
root
.
left
=
buildSearchTree
(
values
,
l
+
1
,
index
-
1
)
root
.
right
=
buildSearchTree
(
values
,
index
,
r
)
root
}
buildSearchTree
(
preorder
,
0
,
preorder
.
length
-
1
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录