Skip to content

Commit 2ad67ef

Browse files
authored
Merge pull request #226 from LetMeFly666/107
update: 对于问题0107:重写了C++代码,新增了Py代码,更新了题解的Py代码
2 parents 6de5c20 + 85097e4 commit 2ad67ef

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-02-15 10:15:09
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-02-15 10:17:37
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>> levelOrderBottom(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+
reverse(ans.begin(), ans.end());
45+
return ans;
46+
}
47+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-02-15 10:18:41
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-02-15 10:22:23
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 levelOrderBottom(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+
ans.reverse()
33+
return ans

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ date: 2022-07-04 23:18:27
44
tags: [题解, LeetCode, 中等, 树, 广度优先搜索, 二叉树, 层次遍历]
55
---
66

7-
# 【LetMeFly】107.二叉树的层序遍历 II
7+
# 【LetMeFly】107.二叉树的层序遍历 II:正常遍历后翻转
88

99
力扣题目链接:[https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
1010

@@ -93,5 +93,39 @@ public:
9393
};
9494
```
9595
96+
#### Python
97+
98+
```python
99+
# from typing import List, Optional
100+
101+
# # Definition for a binary tree node.
102+
# class TreeNode:
103+
# def __init__(self, val=0, left=None, right=None):
104+
# self.val = val
105+
# self.left = left
106+
# self.right = right
107+
108+
class Solution:
109+
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
110+
ans = []
111+
q = []
112+
if root:
113+
q.append(root)
114+
while q:
115+
ans.append([])
116+
for _ in range(len(q)):
117+
thisNode = q[0]
118+
q = q[1:]
119+
ans[-1].append(thisNode.val)
120+
if thisNode.left:
121+
q.append(thisNode.left)
122+
if thisNode.right:
123+
q.append(thisNode.right)
124+
ans.reverse()
125+
return ans
126+
```
127+
128+
其实Python的队列可以使用collections.deque。
129+
96130
> 同步发文于CSDN,原创不易,转载请附上[原文链接](https://blog.tisfy.eu.org/2022/07/04/LeetCode%200107.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86II/)哦~
97131
> Tisfy:[https://letmefly.blog.csdn.net/article/details/125610699](https://letmefly.blog.csdn.net/article/details/125610699)

0 commit comments

Comments
 (0)