Skip to content

Commit dd7da60

Browse files
committed
update: 对于问题0102:重写了C++代码,新增了Py代码,更新了题解
1 parent f7eeaba commit dd7da60

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-02-14 10:10:40
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-02-14 10:11:53
6+
'''
7+
from typing import List, Optional
8+
9+
# Definition for a binary tree node.
10+
class TreeNode:
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
16+
class Solution:
17+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
18+
ans = []
19+
q = []
20+
if root:
21+
q.append(root)
22+
while q:
23+
ans.append([])
24+
for _ in range(len(q)):
25+
thisNode = q[0]
26+
q = q[1:]
27+
ans[-1].append(thisNode.val)
28+
if thisNode.left:
29+
q.append(thisNode.left)
30+
if thisNode.right:
31+
q.append(thisNode.right)
32+
return ans
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-02-14 10:06:41
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-02-14 10:09:52
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for a binary tree node.
13+
* struct TreeNode {
14+
* int val;
15+
* TreeNode *left;
16+
* TreeNode *right;
17+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
20+
* };
21+
*/
22+
class Solution {
23+
public:
24+
vector<vector<int>> levelOrder(TreeNode* root) {
25+
vector<vector<int>> ans;
26+
queue<TreeNode*> q;
27+
if (root) {
28+
q.push(root);
29+
}
30+
while (q.size()) {
31+
ans.push_back({});
32+
for (int i = q.size(); i > 0; i--) {
33+
TreeNode* thisNode = q.front();
34+
q.pop();
35+
ans.back().push_back(thisNode->val);
36+
if (thisNode->left) {
37+
q.push(thisNode->left);
38+
}
39+
if (thisNode->right) {
40+
q.push(thisNode->right);
41+
}
42+
}
43+
}
44+
return ans;
45+
}
46+
};

Solutions/LeetCode 0102.二叉树的层序遍历.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,77 @@ public:
149149
};
150150
```
151151

152+
## 方法三:更简单点的方法(不需要将“那一层”的信息入队)
153+
154+
我们只需要将节点入队,当队列非空时:
155+
156+
> 假如当前队列的大小为$size$,那么就一共循环$size$次,并作为一层加入到答案中。
157+
158+
### AC代码
159+
160+
#### C++
161+
162+
```cpp
163+
class Solution {
164+
public:
165+
vector<vector<int>> levelOrder(TreeNode* root) {
166+
vector<vector<int>> ans;
167+
queue<TreeNode*> q;
168+
if (root) {
169+
q.push(root);
170+
}
171+
while (q.size()) {
172+
ans.push_back({});
173+
for (int i = q.size(); i > 0; i--) {
174+
TreeNode* thisNode = q.front();
175+
q.pop();
176+
ans.back().push_back(thisNode->val);
177+
if (thisNode->left) {
178+
q.push(thisNode->left);
179+
}
180+
if (thisNode->right) {
181+
q.push(thisNode->right);
182+
}
183+
}
184+
}
185+
return ans;
186+
}
187+
};
188+
```
189+
190+
#### Python
191+
192+
```python
193+
# from typing import List, Optional
194+
195+
# # Definition for a binary tree node.
196+
# class TreeNode:
197+
# def __init__(self, val=0, left=None, right=None):
198+
# self.val = val
199+
# self.left = left
200+
# self.right = right
201+
202+
class Solution:
203+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
204+
ans = []
205+
q = []
206+
if root:
207+
q.append(root)
208+
while q:
209+
ans.append([])
210+
for _ in range(len(q)):
211+
thisNode = q[0]
212+
q = q[1:]
213+
ans[-1].append(thisNode.val)
214+
if thisNode.left:
215+
q.append(thisNode.left)
216+
if thisNode.right:
217+
q.append(thisNode.right)
218+
return ans
219+
```
220+
221+
+ 时间复杂度$O(N)$,其中$N$是节点个数
222+
+ 空间复杂度$O(N2)$,其中$N2$是节点最多的一层的节点数
223+
152224
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.tisfy.eu.org/2022/07/03/LeetCode%200102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86/)哦~
153225
> Tisfy:[https://letmefly.blog.csdn.net/article/details/125584554](https://letmefly.blog.csdn.net/article/details/125584554)

0 commit comments

Comments
 (0)