Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
6f372933
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6f372933
编写于
12月 24, 2023
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
level travel bst
上级
cf40a01d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
62 addition
and
9 deletion
+62
-9
README.md
README.md
+2
-1
src/main/java/git/snippet/tree/LeetCode_0116_PopulatingNextRightPointersInEachNode.java
.../LeetCode_0116_PopulatingNextRightPointersInEachNode.java
+5
-4
src/main/java/git/snippet/tree/LeetCode_0117_PopulatingNextRightPointersInEachNodeII.java
...eetCode_0117_PopulatingNextRightPointersInEachNodeII.java
+5
-4
src/main/java/git/snippet/tree/LeetCode_0637_AverageOfLevelsInBinaryTree.java
...ippet/tree/LeetCode_0637_AverageOfLevelsInBinaryTree.java
+50
-0
未找到文件。
README.md
浏览文件 @
6f372933
...
...
@@ -36,7 +36,8 @@
[
二叉树的按层遍历相关问题
](
https://www.cnblogs.com/greyzeng/p/16356829.html
)
-
类似问题一: 二叉树自底向上层序遍历
-
类似问题二:以数组的形式返回每一层节点的平均值
-
类似问题三:填充每个节点的下一个右侧节点指针
## 更多
...
...
src/main/java/git/snippet/
leetcod
e/LeetCode_0116_PopulatingNextRightPointersInEachNode.java
→
src/main/java/git/snippet/
tre
e/LeetCode_0116_PopulatingNextRightPointersInEachNode.java
浏览文件 @
6f372933
package
git.snippet.
leetcod
e
;
package
git.snippet.
tre
e
;
import
java.util.LinkedList
;
import
java.util.Queue
;
// https://leetcode.c
n
/problems/populating-next-right-pointers-in-each-node/description/
// https://leetcode.c
om
/problems/populating-next-right-pointers-in-each-node/description/
// 本质是树的按层遍历
// 1. hash表+LinkedList
// 2. 仅用LinkedList
...
...
@@ -16,7 +16,7 @@ import java.util.Queue;
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
public
class
LeetCode_0116_PopulatingNextRightPointersInEachNode
{
// 使用LinkedList作为队列,空间O(N)
public
static
Node
connect
(
Node
root
)
{
public
Node
connect
(
Node
root
)
{
if
(
null
==
root
)
{
return
null
;
}
...
...
@@ -28,6 +28,7 @@ public class LeetCode_0116_PopulatingNextRightPointersInEachNode {
size
=
queue
.
size
();
// 把每一层用链表串好,pre记录每一层的最后那个位置
// 用于和下一层的开头连接
// 每次把一层搞定
for
(;
size
>
0
;
size
--)
{
Node
p
=
queue
.
poll
();
if
(
pre
!=
null
)
{
...
...
@@ -46,7 +47,7 @@ public class LeetCode_0116_PopulatingNextRightPointersInEachNode {
return
root
;
}
public
static
Node
connect2
(
Node
root
)
{
public
Node
connect2
(
Node
root
)
{
if
(
root
==
null
)
{
return
null
;
}
...
...
src/main/java/git/snippet/
leetcod
e/LeetCode_0117_PopulatingNextRightPointersInEachNodeII.java
→
src/main/java/git/snippet/
tre
e/LeetCode_0117_PopulatingNextRightPointersInEachNodeII.java
浏览文件 @
6f372933
package
git.snippet.
leetcod
e
;
package
git.snippet.
tre
e
;
// 和116题一样
// https://leetcode.c
n
/problems/populating-next-right-pointers-in-each-node-ii/
// https://leetcode.c
om
/problems/populating-next-right-pointers-in-each-node-ii/
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
public
class
LeetCode_0117_PopulatingNextRightPointersInEachNodeII
{
public
static
Node
connect
(
Node
root
)
{
// 采用自定义队列
public
Node
connect
(
Node
root
)
{
if
(
root
==
null
)
{
return
null
;
}
...
...
@@ -33,7 +34,7 @@ public class LeetCode_0117_PopulatingNextRightPointersInEachNodeII {
return
root
;
}
public
static
class
Node
{
public
class
Node
{
public
int
val
;
public
Node
left
;
public
Node
right
;
...
...
src/main/java/git/snippet/
leetcod
e/LeetCode_0637_AverageOfLevelsInBinaryTree.java
→
src/main/java/git/snippet/
tre
e/LeetCode_0637_AverageOfLevelsInBinaryTree.java
浏览文件 @
6f372933
/*
* Given a non-empty binary tree, return the average value of the nodes on each level in the form of
* an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average
* value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5,
* 11].
*/
package
git.snippet.leetcode
;
package
git.snippet.tree
;
import
java.util.ArrayList
;
import
java.util.LinkedList
;
...
...
@@ -12,10 +6,10 @@ import java.util.List;
import
java.util.Queue
;
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
// https://leetcode.c
n
/problems/average-of-levels-in-binary-tree/description/
// https://leetcode.c
om
/problems/average-of-levels-in-binary-tree/description/
// 按层遍历,然后求每层的平均值
public
class
LeetCode_0637_AverageOfLevelsInBinaryTree
{
public
static
List
<
Double
>
averageOfLevels
(
TreeNode
root
)
{
public
List
<
Double
>
averageOfLevels
(
TreeNode
root
)
{
if
(
null
==
root
)
{
return
new
ArrayList
<>();
}
...
...
@@ -23,33 +17,32 @@ public class LeetCode_0637_AverageOfLevelsInBinaryTree {
Queue
<
TreeNode
>
queue
=
new
LinkedList
<>();
TreeNode
curEnd
=
root
;
TreeNode
nextEnd
=
null
;
int
numOfNodes
=
0
;
Double
sumOfNodesVal
=
0
d
;
int
numOfNodes
=
0
;
// 记录每一层的节点个数
double
sumOfNodesVal
=
0
d
;
// 记录每一层的节点平均值(累计值除以个数)
queue
.
offer
(
root
);
while
(!
queue
.
isEmpty
())
{
TreeNode
poll
=
queue
.
poll
();
if
(
null
!=
poll
.
left
)
{
queue
.
offer
(
poll
.
left
);
nextEnd
=
poll
.
left
;
TreeNode
cur
=
queue
.
poll
();
if
(
null
!=
cur
.
left
)
{
queue
.
offer
(
cur
.
left
);
nextEnd
=
cur
.
left
;
}
if
(
null
!=
poll
.
right
)
{
queue
.
offer
(
poll
.
right
);
nextEnd
=
poll
.
right
;
if
(
null
!=
cur
.
right
)
{
queue
.
offer
(
cur
.
right
);
nextEnd
=
cur
.
right
;
}
numOfNodes
++;
sumOfNodesVal
+=
poll
.
val
;
if
(
poll
==
curEnd
)
{
sumOfNodesVal
+=
cur
.
val
;
if
(
cur
==
curEnd
)
{
result
.
add
(
sumOfNodesVal
/
numOfNodes
);
sumOfNodesVal
=
0
d
;
numOfNodes
=
0
;
curEnd
=
nextEnd
;
}
}
return
result
;
}
public
static
class
TreeNode
{
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录