Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
93c01ddd
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,发现更多精彩内容 >>
提交
93c01ddd
编写于
7月 29, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add scala #337
上级
5ce7facf
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
59 addition
and
5 deletion
+59
-5
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_337_Tree.scala
...c/main/scala/io/github/dreamylost/Leetcode_337_Tree.scala
+58
-4
scala-leetcode/src/main/scala/io/github/dreamylost/dreamylost.md
...eetcode/src/main/scala/io/github/dreamylost/dreamylost.md
+1
-1
未找到文件。
scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_337_Tree.scala
浏览文件 @
93c01ddd
...
...
@@ -18,15 +18,69 @@ package io.github.dreamylost
* @time 2018年8月10日
* @version v1.0
*/
object
Leetcode_337_Tree
extends
App
{
object
Leetcode_337_Tree
{
/**
* 对于一个以 root 为根节点的二叉树而言,如果尝试偷取 root 节点,那么势必不能偷取其左右子节点,然后继续尝试偷取其左右子节点的左右子节点。
* 如果不偷取该节点,那么只能尝试偷取其左右子节点比较两种方式的结果,谁大取谁。
*
* 3768 ms,8.33%
* fold 4848ms
* 54.9 MB,100.00%
*
* @param root
* @return
*/
def
rob
(
root
:
TreeNode
)
:
Int
=
{
if
(
root
==
null
)
return
0
val
memory
=
new
scala
.
collection
.
mutable
.
HashMap
[
TreeNode
,
Int
]()
if
(
memory
.
contains
(
root
))
return
memory
(
root
)
var
ccVal
=
root
.
value
if
(
root
.
left
!=
null
)
ccVal
+=
rob
(
root
.
left
.
left
)
+
rob
(
root
.
left
.
right
)
if
(
root
.
right
!=
null
)
ccVal
+=
rob
(
root
.
right
.
left
)
+
rob
(
root
.
right
.
right
)
val
cVal
=
rob
(
root
.
left
)
+
rob
(
root
.
right
)
memory
.
put
(
root
,
ccVal
)
math
.
max
(
cVal
,
ccVal
)
}
/**
* 解决重复子问题,竟然还慢
*
* 1372 ms,33.33%
* 52.2 MB,100.00%
*
* @param root
* @return
*/
def
rob_
(
root
:
TreeNode
)
:
Int
=
{
if
(
root
==
null
)
return
0
var
vart
=
root
.
value
if
(
root
.
left
!=
null
)
vart
+=
rob
(
root
.
left
.
left
)
+
rob
(
root
.
righ
t
.
right
)
if
(
root
.
right
!=
null
)
vart
+=
rob
(
root
.
right
.
righ
t
)
+
rob
(
root
.
right
.
right
)
va
r
valt
=
rob
(
root
.
left
)
+
rob
(
root
.
right
)
if
(
root
.
left
!=
null
)
vart
+=
rob
(
root
.
left
.
left
)
+
rob
(
root
.
lef
t
.
right
)
if
(
root
.
right
!=
null
)
vart
+=
rob
(
root
.
right
.
lef
t
)
+
rob
(
root
.
right
.
right
)
va
l
valt
=
rob
(
root
.
left
)
+
rob
(
root
.
right
)
math
.
max
(
valt
,
vart
)
}
/**
* 后续中递归
*
* 688 ms,100.00%
* 53.1 MB,100.00%
*
* @param root
* @return
*/
def
rob2
(
root
:
TreeNode
)
:
Int
=
{
if
(
root
==
null
)
return
0
def
helper
(
r
:
TreeNode
)
:
Seq
[
Int
]
=
{
if
(
r
==
null
)
return
Seq
(
0
,
0
)
// index 0 存储 下下层,偷root和root的下下层节点
// index 1 存储下层(即不偷当前子树的root,而是偷root的左右子节点)
val
left
=
helper
(
r
.
left
)
val
right
=
helper
(
r
.
right
)
val
h
=
math
.
max
(
left
.
head
+
right
.
head
+
r
.
value
,
left
(
1
)
+
right
(
1
))
Seq
(
left
(
1
)
+
right
(
1
),
h
)
}
helper
(
root
)(
1
)
}
}
scala-leetcode/src/main/scala/io/github/dreamylost/dreamylost.md
浏览文件 @
93c01ddd
...
...
@@ -17,7 +17,7 @@
*
[
统计左叶子节点的和
](
./Leetcode_404_Tree.scala
)
*
[
最小路径
](
./Leetcode_111_Tree.scala
)
*
[
判断路径和是否等于一个数
](
./Leetcode_112_Tree.scala
)
*
[
间隔遍历
](
./Leetcode_337_Tree.scala
)
*
[
间隔遍历
/打家劫舍 III
](
./Leetcode_337_Tree.scala
)
*
[
两节点的最长路径
](
./Leetcode_543_Tree.scala
)
*
[
路径总和 III
](
./Leetcode_437_Tree.scala
)
*
[
二叉搜索树中的众数
](
./Leetcode_501.scala
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录