Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d98ba9f

Browse files
feat: add solutions to lc problem: No.0107 (doocs#2344)
No.0107.Binary Tree Level Order Traversal II
1 parent f94ca5f commit d98ba9f

File tree

7 files changed

+270
-146
lines changed

7 files changed

+270
-146
lines changed

‎solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md‎

Lines changed: 93 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@
4444

4545
### 方法一:BFS
4646

47-
思路同 [102](https://github.com/doocs/leetcode/blob/main/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md),最后反转一下结果即可。
47+
我们可以使用 BFS 的方法来解决这道题。首先将根节点入队,然后不断地进行以下操作,直到队列为空:
48+
49+
- 遍历当前队列中的所有节点,将它们的值存储到一个临时数组 $t$ 中,然后将它们的孩子节点入队。
50+
- 将临时数组 $t$ 存储到答案数组中。
51+
52+
最后将答案数组反转后返回即可。
4853

4954
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
5055

@@ -135,18 +140,24 @@ class Solution {
135140
public:
136141
vector<vector<int>> levelOrderBottom(TreeNode* root) {
137142
vector<vector<int>> ans;
138-
if (!root) return ans;
143+
if (!root) {
144+
return ans;
145+
}
139146
queue<TreeNode*> q{{root}};
140147
while (!q.empty()) {
141148
vector<int> t;
142-
for (int i = q.size(); i; --i) {
149+
for (int n = q.size(); n; --n) {
143150
auto node = q.front();
144151
q.pop();
145-
t.emplace_back(node->val);
146-
if (node->left) q.push(node->left);
147-
if (node->right) q.push(node->right);
152+
t.push_back(node->val);
153+
if (node->left) {
154+
q.push(node->left);
155+
}
156+
if (node->right) {
157+
q.push(node->right);
158+
}
148159
}
149-
ans.emplace_back(t);
160+
ans.push_back(t);
150161
}
151162
reverse(ans.begin(), ans.end());
152163
return ans;
@@ -163,15 +174,14 @@ public:
163174
* Right *TreeNode
164175
* }
165176
*/
166-
func levelOrderBottom(root *TreeNode) [][]int {
167-
ans := [][]int{}
177+
func levelOrderBottom(root *TreeNode) (ans [][]int) {
168178
if root == nil {
169-
return ans
179+
return
170180
}
171181
q := []*TreeNode{root}
172182
for len(q) > 0 {
173-
var t []int
174-
for i := len(q); i > 0; i-- {
183+
t := []int{}
184+
for n := len(q); n > 0; n-- {
175185
node := q[0]
176186
q = q[1:]
177187
t = append(t, node.Val)
@@ -182,9 +192,48 @@ func levelOrderBottom(root *TreeNode) [][]int {
182192
q = append(q, node.Right)
183193
}
184194
}
185-
ans = append([][]int{t}, ans...)
195+
ans = append(ans, t)
196+
}
197+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
198+
ans[i], ans[j] = ans[j], ans[i]
186199
}
187-
return ans
200+
return
201+
}
202+
```
203+
204+
```ts
205+
/**
206+
* Definition for a binary tree node.
207+
* class TreeNode {
208+
* val: number
209+
* left: TreeNode | null
210+
* right: TreeNode | null
211+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
212+
* this.val = (val===undefined ? 0 : val)
213+
* this.left = (left===undefined ? null : left)
214+
* this.right = (right===undefined ? null : right)
215+
* }
216+
* }
217+
*/
218+
219+
function levelOrderBottom(root: TreeNode | null): number[][] {
220+
const ans: number[][] = [];
221+
if (!root) {
222+
return ans;
223+
}
224+
const q: TreeNode[] = [root];
225+
while (q.length) {
226+
const t: number[] = [];
227+
const qq: TreeNode[] = [];
228+
for (const { val, left, right } of q) {
229+
t.push(val);
230+
left && qq.push(left);
231+
right && qq.push(right);
232+
}
233+
ans.push(t);
234+
q.splice(0, q.length, ...qq);
235+
}
236+
return ans.reverse();
188237
}
189238
```
190239

@@ -209,38 +258,30 @@ func levelOrderBottom(root *TreeNode) [][]int {
209258
// }
210259
use std::{ rc::Rc, cell::RefCell, collections::VecDeque };
211260
impl Solution {
212-
#[allow(dead_code)]
213261
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
214-
if root.is_none() {
215-
return vec![];
216-
}
217-
let mut ret_vec = Vec::new();
218-
let mut q = VecDeque::new();
219-
220-
q.push_back(root);
221-
222-
while !q.is_empty() {
223-
let mut cur_vec = Vec::new();
224-
let mut next_q = VecDeque::new();
262+
let mut ans = Vec::new();
263+
if let Some(root_node) = root {
264+
let mut q = VecDeque::new();
265+
q.push_back(root_node);
225266
while !q.is_empty() {
226-
let cur_front = q.front().unwrap().clone();
227-
q.pop_front();
228-
cur_vec.push(cur_front.as_ref().unwrap().borrow().val);
229-
let left = cur_front.as_ref().unwrap().borrow().left.clone();
230-
let right = cur_front.as_ref().unwrap().borrow().right.clone();
231-
if !left.is_none() {
232-
next_q.push_back(left);
233-
}
234-
if !right.is_none() {
235-
next_q.push_back(right);
267+
let mut t = Vec::new();
268+
for _ in 0..q.len() {
269+
if let Some(node) = q.pop_front() {
270+
let node_ref = node.borrow();
271+
t.push(node_ref.val);
272+
if let Some(ref left) = node_ref.left {
273+
q.push_back(Rc::clone(left));
274+
}
275+
if let Some(ref right) = node_ref.right {
276+
q.push_back(Rc::clone(right));
277+
}
278+
}
236279
}
280+
ans.push(t);
237281
}
238-
ret_vec.push(cur_vec);
239-
q = next_q;
240282
}
241-
242-
ret_vec.reverse();
243-
ret_vec
283+
ans.reverse();
284+
ans
244285
}
245286
}
246287
```
@@ -260,19 +301,22 @@ impl Solution {
260301
*/
261302
var levelOrderBottom = function (root) {
262303
const ans = [];
263-
if (!root) return ans;
304+
if (!root) {
305+
return ans;
306+
}
264307
const q = [root];
265308
while (q.length) {
266309
const t = [];
267-
for (let i =q.length; i >0; --i) {
268-
constnode=q.shift();
269-
t.push(node.val);
270-
if (node.left) q.push(node.left);
271-
if (node.right) q.push(node.right);
310+
constqq= [];
311+
for (const { val, left, right } of q) {
312+
t.push(val);
313+
left&&qq.push(left);
314+
right&&qq.push(right);
272315
}
273-
ans.unshift(t);
316+
ans.push(t);
317+
q.splice(0, q.length, ...qq);
274318
}
275-
return ans;
319+
return ans.reverse();
276320
};
277321
```
278322

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /