Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
一梦清浅…
leetcode
提交
eb428505
L
leetcode
项目概览
一梦清浅…
/
leetcode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
eb428505
编写于
5月 15, 2019
作者:
L
liu13
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
20190515
上级
c86a4ba9
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
178 addition
and
0 deletion
+178
-0
code/lc559.java
code/lc559.java
+36
-0
code/lc589.java
code/lc589.java
+41
-0
code/lc590.java
code/lc590.java
+33
-0
code/lc834.java
code/lc834.java
+64
-0
readme.md
readme.md
+4
-0
未找到文件。
code/lc559.java
0 → 100644
浏览文件 @
eb428505
package
code
;
import
java.util.List
;
/*
* 559. Maximum Depth of N-ary Tree
* 题意:多叉树的最大深度
* 难度:Easy
* 分类:Tree, Depth-first Search, Breadth-first Search
* 思路:BFS 也能做和 lc104思路一样
* Tips:lc104, lc111
*/
public
class
lc559
{
class
Node
{
public
int
val
;
public
List
<
Node
>
children
;
public
Node
()
{}
public
Node
(
int
_val
,
List
<
Node
>
_children
)
{
val
=
_val
;
children
=
_children
;
}
}
public
int
maxDepth
(
Node
root
)
{
if
(
root
==
null
)
return
0
;
int
res
=
0
;
for
(
Node
nd
:
root
.
children
)
{
res
=
Math
.
max
(
maxDepth
(
nd
),
res
);
}
return
res
+
1
;
}
}
code/lc589.java
0 → 100644
浏览文件 @
eb428505
package
code
;
import
java.util.ArrayList
;
import
java.util.List
;
/*
* 589. N-ary Tree Preorder Traversal
* 题意:多叉树先序遍历
* 难度:Easy
* 分类:Tree
* 思路:
* Tips:
*/
public
class
lc589
{
class
Node
{
public
int
val
;
public
List
<
Node
>
children
;
public
Node
()
{}
public
Node
(
int
_val
,
List
<
Node
>
_children
)
{
val
=
_val
;
children
=
_children
;
}
}
List
<
Integer
>
res
;
public
List
<
Integer
>
preorder
(
Node
root
)
{
res
=
new
ArrayList
<>();
helper
(
root
);
return
res
;
}
public
void
helper
(
Node
root
){
if
(
root
==
null
)
return
;
res
.
add
(
root
.
val
);
for
(
Node
nd:
root
.
children
){
helper
(
nd
);
}
}
}
code/lc590.java
0 → 100644
浏览文件 @
eb428505
package
code
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
lc590
{
class
Node
{
public
int
val
;
public
List
<
Node
>
children
;
public
Node
()
{}
public
Node
(
int
_val
,
List
<
Node
>
_children
)
{
val
=
_val
;
children
=
_children
;
}
}
List
<
Integer
>
res
;
public
List
<
Integer
>
postorder
(
Node
root
)
{
res
=
new
ArrayList
<>();
helper
(
root
);
return
res
;
}
public
void
helper
(
Node
root
){
if
(
root
==
null
)
return
;
for
(
Node
nd:
root
.
children
){
helper
(
nd
);
}
res
.
add
(
root
.
val
);
}
}
code/lc834.java
0 → 100644
浏览文件 @
eb428505
package
code
;
import
java.util.HashMap
;
import
java.util.HashSet
;
/*
* 834. Sum of Distances in Tree
* 题意:求树中每个节点的值,到其他节点的距离和
* 难度:Hard
* 分类:Tree, Depth-first Search
* 思路:真的难,我是做不出来
* 两次遍历,第一次后续遍历,计算出每个节点为根的树,有几个孩子节点,并计算出root的结果
* 根据前一步的计算结果,开始先序遍历,一步步计算出其他节点的结果
* res[node] = res[parent]-2*count[node]+count.length;
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130583/C%2B%2BJavaPython-Pre-order-and-Post-order-DFS-O(N)
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130567/Two-traversals-O(N)-python-solution-with-Explanation
* Tips:
*/
public
class
lc834
{
public
static
void
main
(
String
[]
args
)
{
int
[][]
arr
=
{{
0
,
1
},{
0
,
2
},{
2
,
3
},{
2
,
4
},{
2
,
5
}};
sumOfDistancesInTree
(
6
,
arr
);
}
static
int
[]
count
;
static
int
[]
res
;
static
HashMap
<
Integer
,
HashSet
<
Integer
>>
hm
=
new
HashMap
();
public
static
int
[]
sumOfDistancesInTree
(
int
N
,
int
[][]
edges
)
{
if
(
edges
.
length
==
0
)
return
new
int
[]{
0
};
count
=
new
int
[
N
];
//记录n为跟的树,下边有多少个节点
res
=
new
int
[
N
];
for
(
int
i
=
0
;
i
<
edges
.
length
;
i
++)
{
//双向都添加,遍历的时候判断一下,因为并不一定0就是根
HashSet
<
Integer
>
hs
=
hm
.
getOrDefault
(
edges
[
i
][
0
],
new
HashSet
());
hs
.
add
(
edges
[
i
][
1
]);
hm
.
put
(
edges
[
i
][
0
],
hs
);
hs
=
hm
.
getOrDefault
(
edges
[
i
][
1
],
new
HashSet
());
hs
.
add
(
edges
[
i
][
0
]);
hm
.
put
(
edges
[
i
][
1
],
hs
);
}
helper1
(
0
,
-
1
);
helper2
(
0
,
-
1
);
return
res
;
}
public
static
int
helper1
(
int
node
,
int
parent
){
//后序遍历,求root对应的结果,计算每个节点的count
HashSet
<
Integer
>
hs
=
hm
.
getOrDefault
(
node
,
new
HashSet
());
for
(
Integer
i:
hs
)
{
if
(
i
==
parent
)
continue
;
//是parent的话直接略过
count
[
node
]
+=
helper1
(
i
,
node
);
res
[
node
]
+=
res
[
i
]
+
count
[
i
];
}
count
[
node
]++;
return
count
[
node
];
}
public
static
void
helper2
(
int
node
,
int
parent
){
//先序遍历,求结果
HashSet
<
Integer
>
hs
=
hm
.
getOrDefault
(
node
,
new
HashSet
());
if
(
node
!=
0
)
res
[
node
]
=
res
[
parent
]-
2
*
count
[
node
]+
count
.
length
;
//转移计算
for
(
Integer
i:
hs
)
{
if
(
i
==
parent
)
continue
;
//是parent的话直接略过
helper2
(
i
,
node
);
}
}
}
readme.md
浏览文件 @
eb428505
...
...
@@ -212,9 +212,12 @@ LeetCode 指南
| 494
[
Java
](
./code/lc494.java
)
| 538
[
Java
](
./code/lc538.java
)
| 543
[
Java
](
./code/lc543.java
)
| 559
[
Java
](
./code/lc559.java
)
| 560
[
Java
](
./code/lc543.java
)
| 572
[
Java
](
./code/lc572.java
)
| 581
[
Java
](
./code/lc581.java
)
| 589
[
Java
](
./code/lc589.java
)
| 590
[
Java
](
./code/lc590.java
)
| 617
[
Java
](
./code/lc617.java
)
| 621
[
Java
](
./code/lc621.java
)
| 647
[
Java
](
./code/lc647.java
)
...
...
@@ -222,6 +225,7 @@ LeetCode 指南
| 714
[
Java
](
./code/lc714.java
)
| 746
[
Java
](
./code/lc746.java
)
| 771
[
Java
](
./code/lc771.java
)
| 834
[
Java
](
./code/lc834.java
)
| 877
[
Java
](
./code/lc877.java
)
| 921
[
Java
](
./code/lc921.java
)
| 922
[
Java
](
./code/lc922.java
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录