Skip to content

Commit cca8a3f

Browse files
author
luzhipeng
committed
bfs
1 parent eb1a92a commit cca8a3f

3 files changed

+263
-0
lines changed

Diff for: 102.binary-tree-level-order-traversal.md

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/binary-tree-level-order-traversal/description/
4+
5+
## 题目描述
6+
```
7+
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
8+
9+
For example:
10+
Given binary tree [3,9,20,null,null,15,7],
11+
3
12+
/ \
13+
9 20
14+
/ \
15+
15 7
16+
return its level order traversal as:
17+
[
18+
[3],
19+
[9,20],
20+
[15,7]
21+
]
22+
```
23+
24+
## 思路
25+
26+
这道题可以借助`队列`实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。
27+
28+
29+
然后就是while(queue.length), 每次处理一个节点,都将其子节点(在这里是left和right)放到队列中。
30+
31+
然后不断的出队, 如果出队的是null,则表式这一层已经结束了,我们就继续push一个null。
32+
33+
34+
## 关键点解析
35+
36+
- 队列
37+
38+
- 队列中用Null(一个特殊元素)来划分每层
39+
40+
- 树的基本操作- 遍历 - 层次遍历(BFS)
41+
42+
43+
## 代码
44+
45+
```js
46+
/*
47+
* @lc app=leetcode id=102 lang=javascript
48+
*
49+
* [102] Binary Tree Level Order Traversal
50+
*
51+
* https://leetcode.com/problems/binary-tree-level-order-traversal/description/
52+
*
53+
* algorithms
54+
* Medium (47.18%)
55+
* Total Accepted: 346.4K
56+
* Total Submissions: 731.3K
57+
* Testcase Example: '[3,9,20,null,null,15,7]'
58+
*
59+
* Given a binary tree, return the level order traversal of its nodes' values.
60+
* (ie, from left to right, level by level).
61+
*
62+
*
63+
* For example:
64+
* Given binary tree [3,9,20,null,null,15,7],
65+
*
66+
*
67+
* ⁠ 3
68+
* ⁠ / \
69+
* ⁠ 9 20
70+
* ⁠ / \
71+
* ⁠ 15 7
72+
*
73+
*
74+
*
75+
* return its level order traversal as:
76+
*
77+
* [
78+
* ⁠ [3],
79+
* ⁠ [9,20],
80+
* ⁠ [15,7]
81+
* ]
82+
*
83+
*
84+
*/
85+
/**
86+
* Definition for a binary tree node.
87+
* function TreeNode(val) {
88+
* this.val = val;
89+
* this.left = this.right = null;
90+
* }
91+
*/
92+
/**
93+
* @param {TreeNode} root
94+
* @return {number[][]}
95+
*/
96+
var levelOrder = function(root) {
97+
if (!root) return [];
98+
const items = []; // 存放所有节点
99+
const queue = [root, null]; // null 简化操作
100+
let levelNodes = []; // 存放每一层的节点
101+
102+
while (queue.length > 0) {
103+
const t = queue.shift();
104+
105+
if (t) {
106+
levelNodes.push(t.val)
107+
if (t.left) {
108+
queue.push(t.left);
109+
}
110+
if (t.right) {
111+
queue.push(t.right);
112+
}
113+
} else { // 一层已经遍历完了
114+
items.push(levelNodes);
115+
levelNodes = [];
116+
if (queue.length > 0) {
117+
queue.push(null)
118+
}
119+
}
120+
}
121+
122+
return items;
123+
};
124+
125+
```

Diff for: 103.binary-tree-zigzag-level-order-traversal.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
## 题目地址
3+
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
4+
5+
## 题目描述
6+
和leetcode 102 基本是一样的,思路是完全一样的。
7+
8+
```
9+
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).
10+
11+
For example:
12+
Given binary tree [3,9,20,null,null,15,7],
13+
3
14+
/ \
15+
9 20
16+
/ \
17+
15 7
18+
return its zigzag level order traversal as:
19+
[
20+
[3],
21+
[20,9],
22+
[15,7]
23+
]
24+
```
25+
26+
## 思路
27+
28+
这道题可以借助`队列`实现,首先把root入队,然后入队一个特殊元素Null(来表示每层的结束)。
29+
30+
31+
然后就是while(queue.length), 每次处理一个节点,都将其子节点(在这里是left和right)放到队列中。
32+
33+
然后不断的出队, 如果出队的是null,则表式这一层已经结束了,我们就继续push一个null。
34+
35+
36+
## 关键点解析
37+
38+
- 队列
39+
40+
- 队列中用Null(一个特殊元素)来划分每层
41+
42+
- 树的基本操作- 遍历 - 层次遍历(BFS)
43+
44+
45+
## 代码
46+
47+
```js
48+
/*
49+
* @lc app=leetcode id=103 lang=javascript
50+
*
51+
* [103] Binary Tree Zigzag Level Order Traversal
52+
*
53+
* https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
54+
*
55+
* algorithms
56+
* Medium (40.57%)
57+
* Total Accepted: 201.2K
58+
* Total Submissions: 493.7K
59+
* Testcase Example: '[3,9,20,null,null,15,7]'
60+
*
61+
* Given a binary tree, return the zigzag level order traversal of its nodes'
62+
* values. (ie, from left to right, then right to left for the next level and
63+
* alternate between).
64+
*
65+
*
66+
* For example:
67+
* Given binary tree [3,9,20,null,null,15,7],
68+
*
69+
* ⁠ 3
70+
* ⁠ / \
71+
* ⁠ 9 20
72+
* ⁠ / \
73+
* ⁠ 15 7
74+
*
75+
*
76+
*
77+
* return its zigzag level order traversal as:
78+
*
79+
* [
80+
* ⁠ [3],
81+
* ⁠ [20,9],
82+
* ⁠ [15,7]
83+
* ]
84+
*
85+
*
86+
*/
87+
/**
88+
* Definition for a binary tree node.
89+
* function TreeNode(val) {
90+
* this.val = val;
91+
* this.left = this.right = null;
92+
* }
93+
*/
94+
/**
95+
* @param {TreeNode} root
96+
* @return {number[][]}
97+
*/
98+
var zigzagLevelOrder = function(root) {
99+
if (!root) return [];
100+
const items = [];
101+
let isOdd = true;
102+
let levelNodes = [];
103+
104+
const queue = [root, null];
105+
106+
107+
while(queue.length > 0) {
108+
const t = queue.shift();
109+
110+
if (t) {
111+
levelNodes.push(t.val)
112+
if (t.left) {
113+
queue.push(t.left)
114+
}
115+
if (t.right) {
116+
queue.push(t.right)
117+
}
118+
} else {
119+
if (!isOdd) {
120+
levelNodes = levelNodes.reverse();
121+
}
122+
items.push(levelNodes)
123+
levelNodes = [];
124+
isOdd = !isOdd;
125+
if (queue.length > 0) {
126+
queue.push(null);
127+
}
128+
}
129+
}
130+
131+
return items
132+
133+
};
134+
```
135+

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ leetcode题解,记录自己的leecode解题之路。
1616
- [75.sort-colors.md](https://github.com/azl397985856/leetcode/blob/master/75.sort-colors.md)
1717
- [86.partition-list](./86.partition-list.md)
1818
- [92.reverse-linked-list-ii](./92.reverse-linked-list-ii.md)
19+
- [94.binary-tree-inorder-traversal](./94.binary-tree-inorder-traversal.md)
20+
- [102.binary-tree-level-order-traversal](./102.binary-tree-level-order-traversal.md)
21+
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
1922
### 高级难度

0 commit comments

Comments
 (0)