Skip to content

Commit 2e398c6

Browse files
authored
feat: update solutions to lcof problems: No.32.1,34 (doocs#2875)
1 parent b552939 commit 2e398c6

File tree

11 files changed

+124
-247
lines changed

11 files changed

+124
-247
lines changed

lcof/面试题32 - I. 从上到下打印二叉树/README.md

+28-24
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,21 @@ func levelOrder(root *TreeNode) (ans []int) {
211211
*/
212212

213213
function levelOrder(root: TreeNode | null): number[] {
214-
const res = [];
215-
if (root == null) {
216-
return res;
214+
const ans: number[] = [];
215+
if (!root) {
216+
return ans;
217217
}
218-
const queue = [root];
219-
while (queue.length !== 0) {
220-
const { val, left, right } = queue.shift();
221-
res.push(val);
222-
left && queue.push(left);
223-
right && queue.push(right);
218+
const q: TreeNode[] = [root];
219+
while (q.length) {
220+
const t: TreeNode[] = [];
221+
for (const { val, left, right } of q) {
222+
ans.push(val);
223+
left && t.push(left);
224+
right && t.push(right);
225+
}
226+
q.splice(0, q.length, ...t);
224227
}
225-
return res;
228+
return ans;
226229
}
227230
```
228231

@@ -252,22 +255,22 @@ use std::cell::RefCell;
252255
use std::collections::VecDeque;
253256
impl Solution {
254257
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
255-
let mut res = Vec::new();
256-
let mut queue = VecDeque::new();
258+
let mut ans = Vec::new();
259+
let mut q = VecDeque::new();
257260
if let Some(node) = root {
258-
queue.push_back(node);
261+
q.push_back(node);
259262
}
260-
while let Some(node) = queue.pop_front() {
263+
while let Some(node) = q.pop_front() {
261264
let mut node = node.borrow_mut();
262-
res.push(node.val);
265+
ans.push(node.val);
263266
if let Some(l) = node.left.take() {
264-
queue.push_back(l);
267+
q.push_back(l);
265268
}
266269
if let Some(r) = node.right.take() {
267-
queue.push_back(r);
270+
q.push_back(r);
268271
}
269272
}
270-
res
273+
ans
271274
}
272275
}
273276
```
@@ -287,18 +290,19 @@ impl Solution {
287290
* @return {number[]}
288291
*/
289292
var levelOrder = function (root) {
293+
const ans = [];
290294
if (!root) {
291-
return [];
295+
return ans;
292296
}
293297
const q = [root];
294-
const ans = [];
295298
while (q.length) {
296-
for (let n = q.length; n; --n) {
297-
const { val, left, right } = q.shift();
299+
const t = [];
300+
for (const { val, left, right } of q) {
298301
ans.push(val);
299-
left && q.push(left);
300-
right && q.push(right);
302+
left && t.push(left);
303+
right && t.push(right);
301304
}
305+
q.splice(0, q.length, ...t);
302306
}
303307
return ans;
304308
};

lcof/面试题32 - I. 从上到下打印二叉树/Solution.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
* @return {number[]}
1111
*/
1212
var levelOrder = function (root) {
13+
const ans = [];
1314
if (!root) {
14-
return [];
15+
return ans;
1516
}
1617
const q = [root];
17-
const ans = [];
1818
while (q.length) {
19-
for (let n = q.length; n; --n) {
20-
const { val, left, right } = q.shift();
19+
const t = [];
20+
for (const { val, left, right } of q) {
2121
ans.push(val);
22-
left && q.push(left);
23-
right && q.push(right);
22+
left && t.push(left);
23+
right && t.push(right);
2424
}
25+
q.splice(0, q.length, ...t);
2526
}
2627
return ans;
2728
};

lcof/面试题32 - I. 从上到下打印二叉树/Solution.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ use std::cell::RefCell;
2121
use std::collections::VecDeque;
2222
impl Solution {
2323
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
24-
let mut res = Vec::new();
25-
let mut queue = VecDeque::new();
24+
let mut ans = Vec::new();
25+
let mut q = VecDeque::new();
2626
if let Some(node) = root {
27-
queue.push_back(node);
27+
q.push_back(node);
2828
}
29-
while let Some(node) = queue.pop_front() {
29+
while let Some(node) = q.pop_front() {
3030
let mut node = node.borrow_mut();
31-
res.push(node.val);
31+
ans.push(node.val);
3232
if let Some(l) = node.left.take() {
33-
queue.push_back(l);
33+
q.push_back(l);
3434
}
3535
if let Some(r) = node.right.take() {
36-
queue.push_back(r);
36+
q.push_back(r);
3737
}
3838
}
39-
res
39+
ans
4040
}
4141
}

lcof/面试题32 - I. 从上到下打印二叉树/Solution.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
*/
1414

1515
function levelOrder(root: TreeNode | null): number[] {
16-
const res = [];
17-
if (root == null) {
18-
return res;
16+
const ans: number[] = [];
17+
if (!root) {
18+
return ans;
1919
}
20-
const queue = [root];
21-
while (queue.length !== 0) {
22-
const { val, left, right } = queue.shift();
23-
res.push(val);
24-
left && queue.push(left);
25-
right && queue.push(right);
20+
const q: TreeNode[] = [root];
21+
while (q.length) {
22+
const t: TreeNode[] = [];
23+
for (const { val, left, right } of q) {
24+
ans.push(val);
25+
left && t.push(left);
26+
right && t.push(right);
27+
}
28+
q.splice(0, q.length, ...t);
2629
}
27-
return res;
30+
return ans;
2831
}

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+33-106
Original file line numberDiff line numberDiff line change
@@ -228,26 +228,26 @@ func pathSum(root *TreeNode, target int) (ans [][]int) {
228228
*/
229229

230230
function pathSum(root: TreeNode | null, target: number): number[][] {
231-
const res: number[][] = [];
232-
if (root == null) {
233-
return res;
234-
}
235-
const paths: number[] = [];
236-
const dfs = ({ val, right, left }: TreeNode, target: number) => {
237-
paths.push(val);
238-
target -= val;
239-
if (left == null && right == null) {
240-
if (target === 0) {
241-
res.push([...paths]);
242-
}
243-
} else {
244-
left && dfs(left, target);
245-
right && dfs(right, target);
231+
const ans: number[][] = [];
232+
const t: number[] = [];
233+
234+
const dfs = (root: TreeNode | null, s: number): void => {
235+
if (!root) {
236+
return;
237+
}
238+
const { val, left, right } = root;
239+
t.push(val);
240+
s -= val;
241+
if (!left && !right && s === 0) {
242+
ans.push([...t]);
246243
}
247-
paths.pop();
244+
dfs(left, s);
245+
dfs(right, s);
246+
t.pop();
248247
};
248+
249249
dfs(root, target);
250-
return res;
250+
return ans;
251251
}
252252
```
253253

@@ -278,26 +278,26 @@ impl Solution {
278278
fn dfs(
279279
root: &Option<Rc<RefCell<TreeNode>>>,
280280
mut target: i32,
281-
paths: &mut Vec<i32>,
282-
res: &mut Vec<Vec<i32>>
281+
t: &mut Vec<i32>,
282+
ans: &mut Vec<Vec<i32>>
283283
) {
284284
if let Some(node) = root.as_ref() {
285285
let node = node.borrow();
286-
paths.push(node.val);
286+
t.push(node.val);
287287
target -= node.val;
288288
if node.left.is_none() && node.right.is_none() && target == 0 {
289-
res.push(paths.clone());
289+
ans.push(t.clone());
290290
}
291-
Self::dfs(&node.left, target, paths, res);
292-
Self::dfs(&node.right, target, paths, res);
293-
paths.pop();
291+
Self::dfs(&node.left, target, t, ans);
292+
Self::dfs(&node.right, target, t, ans);
293+
t.pop();
294294
}
295295
}
296296

297297
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
298-
let mut res = vec![];
299-
Self::dfs(&root, target, &mut vec![], &mut res);
300-
res
298+
let mut ans = vec![];
299+
Self::dfs(&root, target, &mut vec![], &mut ans);
300+
ans
301301
}
302302
}
303303
```
@@ -325,13 +325,14 @@ var pathSum = function (root, target) {
325325
if (!root) {
326326
return;
327327
}
328-
t.push(root.val);
329-
s -= root.val;
330-
if (!root.left && !root.right && !s) {
328+
const { val, left, right } = root;
329+
t.push(val);
330+
s -= val;
331+
if (!left && !right && !s) {
331332
ans.push([...t]);
332333
}
333-
dfs(root.left, s);
334-
dfs(root.right, s);
334+
dfs(left, s);
335+
dfs(right, s);
335336
t.pop();
336337
};
337338
dfs(root, target);
@@ -384,78 +385,4 @@ public class Solution {
384385

385386
<!-- solution:end -->
386387

387-
<!-- solution:start-->
388-
389-
### 方法二
390-
391-
<!-- tabs:start -->
392-
393-
#### Rust
394-
395-
```rust
396-
// Definition for a binary tree node.
397-
// #[derive(Debug, PartialEq, Eq)]
398-
// pub struct TreeNode {
399-
// pub val: i32,
400-
// pub left: Option<Rc<RefCell<TreeNode>>>,
401-
// pub right: Option<Rc<RefCell<TreeNode>>>,
402-
// }
403-
//
404-
// impl TreeNode {
405-
// #[inline]
406-
// pub fn new(val: i32) -> Self {
407-
// TreeNode {
408-
// val,
409-
// left: None,
410-
// right: None
411-
// }
412-
// }
413-
// }
414-
use std::rc::Rc;
415-
use std::cell::RefCell;
416-
impl Solution {
417-
fn dfs(
418-
root: &Option<Rc<RefCell<TreeNode>>>,
419-
mut target: i32,
420-
paths: &mut Vec<i32>
421-
) -> Vec<Vec<i32>> {
422-
let node = root.as_ref().unwrap().borrow();
423-
paths.push(node.val);
424-
target -= node.val;
425-
let mut res = vec![];
426-
// 确定叶结点身份
427-
if node.left.is_none() && node.right.is_none() {
428-
if target == 0 {
429-
res.push(paths.clone());
430-
}
431-
} else {
432-
if node.left.is_some() {
433-
let res_l = Self::dfs(&node.left, target, paths);
434-
if !res_l.is_empty() {
435-
res = [res, res_l].concat();
436-
}
437-
}
438-
if node.right.is_some() {
439-
let res_r = Self::dfs(&node.right, target, paths);
440-
if !res_r.is_empty() {
441-
res = [res, res_r].concat();
442-
}
443-
}
444-
}
445-
paths.pop();
446-
res
447-
}
448-
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
449-
if root.is_none() {
450-
return vec![];
451-
}
452-
Self::dfs(&root, target, &mut vec![])
453-
}
454-
}
455-
```
456-
457-
<!-- tabs:end -->
458-
459-
<!-- solution:end -->
460-
461388
<!-- problem:end -->

lcof/面试题34. 二叉树中和为某一值的路径/Solution.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ var pathSum = function (root, target) {
1818
if (!root) {
1919
return;
2020
}
21-
t.push(root.val);
22-
s -= root.val;
23-
if (!root.left && !root.right && !s) {
21+
const { val, left, right } = root;
22+
t.push(val);
23+
s -= val;
24+
if (!left && !right && !s) {
2425
ans.push([...t]);
2526
}
26-
dfs(root.left, s);
27-
dfs(root.right, s);
27+
dfs(left, s);
28+
dfs(right, s);
2829
t.pop();
2930
};
3031
dfs(root, target);

0 commit comments

Comments
 (0)