Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
cca8a3f6
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
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,体验更适合开发者的 AI 搜索 >>
提交
cca8a3f6
编写于
3月 22, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bfs
上级
eb1a92a3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
263 addition
and
0 deletion
+263
-0
102.binary-tree-level-order-traversal.md
102.binary-tree-level-order-traversal.md
+125
-0
103.binary-tree-zigzag-level-order-traversal.md
103.binary-tree-zigzag-level-order-traversal.md
+135
-0
README.md
README.md
+3
-0
未找到文件。
102.binary-tree-level-order-traversal.md
0 → 100644
浏览文件 @
cca8a3f6
## 题目地址
https://leetcode.com/problems/binary-tree-level-order-traversal/description/
## 题目描述
```
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
```
## 思路
这道题可以借助
`队列`
实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。
然后就是while(queue.length), 每次处理一个节点,都将其子节点(在这里是left和right)放到队列中。
然后不断的出队, 如果出队的是null,则表式这一层已经结束了,我们就继续push一个null。
## 关键点解析
-
队列
-
队列中用Null(一个特殊元素)来划分每层
-
树的基本操作- 遍历 - 层次遍历(BFS)
## 代码
```
js
/*
* @lc app=leetcode id=102 lang=javascript
*
* [102] Binary Tree Level Order Traversal
*
* https://leetcode.com/problems/binary-tree-level-order-traversal/description/
*
* algorithms
* Medium (47.18%)
* Total Accepted: 346.4K
* Total Submissions: 731.3K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* Given a binary tree, return the level order traversal of its nodes' values.
* (ie, from left to right, level by level).
*
*
* For example:
* Given binary tree [3,9,20,null,null,15,7],
*
*
* 3
* / \
* 9 20
* / \
* 15 7
*
*
*
* return its level order traversal as:
*
* [
* [3],
* [9,20],
* [15,7]
* ]
*
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var
levelOrder
=
function
(
root
)
{
if
(
!
root
)
return
[];
const
items
=
[];
// 存放所有节点
const
queue
=
[
root
,
null
];
// null 简化操作
let
levelNodes
=
[];
// 存放每一层的节点
while
(
queue
.
length
>
0
)
{
const
t
=
queue
.
shift
();
if
(
t
)
{
levelNodes
.
push
(
t
.
val
)
if
(
t
.
left
)
{
queue
.
push
(
t
.
left
);
}
if
(
t
.
right
)
{
queue
.
push
(
t
.
right
);
}
}
else
{
// 一层已经遍历完了
items
.
push
(
levelNodes
);
levelNodes
=
[];
if
(
queue
.
length
>
0
)
{
queue
.
push
(
null
)
}
}
}
return
items
;
};
```
103.binary-tree-zigzag-level-order-traversal.md
0 → 100644
浏览文件 @
cca8a3f6
## 题目地址
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
## 题目描述
和leetcode 102 基本是一样的,思路是完全一样的。
```
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
```
## 思路
这道题可以借助
`队列`
实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。
然后就是while(queue.length), 每次处理一个节点,都将其子节点(在这里是left和right)放到队列中。
然后不断的出队, 如果出队的是null,则表式这一层已经结束了,我们就继续push一个null。
## 关键点解析
-
队列
-
队列中用Null(一个特殊元素)来划分每层
-
树的基本操作- 遍历 - 层次遍历(BFS)
## 代码
```
js
/*
* @lc app=leetcode id=103 lang=javascript
*
* [103] Binary Tree Zigzag Level Order Traversal
*
* https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
*
* algorithms
* Medium (40.57%)
* Total Accepted: 201.2K
* Total Submissions: 493.7K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* Given a binary tree, return the zigzag level order traversal of its nodes'
* values. (ie, from left to right, then right to left for the next level and
* alternate between).
*
*
* For example:
* Given binary tree [3,9,20,null,null,15,7],
*
* 3
* / \
* 9 20
* / \
* 15 7
*
*
*
* return its zigzag level order traversal as:
*
* [
* [3],
* [20,9],
* [15,7]
* ]
*
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var
zigzagLevelOrder
=
function
(
root
)
{
if
(
!
root
)
return
[];
const
items
=
[];
let
isOdd
=
true
;
let
levelNodes
=
[];
const
queue
=
[
root
,
null
];
while
(
queue
.
length
>
0
)
{
const
t
=
queue
.
shift
();
if
(
t
)
{
levelNodes
.
push
(
t
.
val
)
if
(
t
.
left
)
{
queue
.
push
(
t
.
left
)
}
if
(
t
.
right
)
{
queue
.
push
(
t
.
right
)
}
}
else
{
if
(
!
isOdd
)
{
levelNodes
=
levelNodes
.
reverse
();
}
items
.
push
(
levelNodes
)
levelNodes
=
[];
isOdd
=
!
isOdd
;
if
(
queue
.
length
>
0
)
{
queue
.
push
(
null
);
}
}
}
return
items
};
```
README.md
浏览文件 @
cca8a3f6
...
...
@@ -16,4 +16,7 @@ leetcode题解,记录自己的leecode解题之路。
-
[
75.sort-colors.md
](
https://github.com/azl397985856/leetcode/blob/master/75.sort-colors.md
)
-
[
86.partition-list
](
./86.partition-list.md
)
-
[
92.reverse-linked-list-ii
](
./92.reverse-linked-list-ii.md
)
-
[
94.binary-tree-inorder-traversal
](
./94.binary-tree-inorder-traversal.md
)
-
[
102.binary-tree-level-order-traversal
](
./102.binary-tree-level-order-traversal.md
)
-
[
103.binary-tree-zigzag-level-order-traversal
](
./103.binary-tree-zigzag-level-order-traversal.md
)
### 高级难度
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录