Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lcof problems: No.32.1,34 #2875

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions lcof/面试题32 - I. 从上到下打印二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,21 @@ func levelOrder(root *TreeNode) (ans []int) {
*/

function levelOrder(root: TreeNode | null): number[] {
const res = [];
if (root == null) {
return res;
const ans: number[] = [];
if (!root) {
return ans;
}
const queue = [root];
while (queue.length !== 0) {
const { val, left, right } = queue.shift();
res.push(val);
left && queue.push(left);
right && queue.push(right);
const q: TreeNode[] = [root];
while (q.length) {
const t: TreeNode[] = [];
for (const { val, left, right } of q) {
ans.push(val);
left && t.push(left);
right && t.push(right);
}
q.splice(0, q.length, ...t);
}
return res;
return ans;
}
```

Expand Down Expand Up @@ -252,22 +255,22 @@ use std::cell::RefCell;
use std::collections::VecDeque;
impl Solution {
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = Vec::new();
let mut queue = VecDeque::new();
let mut ans = Vec::new();
let mut q = VecDeque::new();
if let Some(node) = root {
queue.push_back(node);
q.push_back(node);
}
while let Some(node) = queue.pop_front() {
while let Some(node) = q.pop_front() {
let mut node = node.borrow_mut();
res.push(node.val);
ans.push(node.val);
if let Some(l) = node.left.take() {
queue.push_back(l);
q.push_back(l);
}
if let Some(r) = node.right.take() {
queue.push_back(r);
q.push_back(r);
}
}
res
ans
}
}
```
Expand All @@ -287,18 +290,19 @@ impl Solution {
* @return {number[]}
*/
var levelOrder = function (root) {
const ans = [];
if (!root) {
return [];
return ans;
}
const q = [root];
const ans = [];
while (q.length) {
for (let n = q.length; n; --n) {
const { val, left, right } = q.shift();
const t = [];
for (const { val, left, right } of q) {
ans.push(val);
left && q.push(left);
right && q.push(right);
left && t.push(left);
right && t.push(right);
}
q.splice(0, q.length, ...t);
}
return ans;
};
Expand Down
13 changes: 7 additions & 6 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
* @return {number[]}
*/
var levelOrder = function (root) {
const ans = [];
if (!root) {
return [];
return ans;
}
const q = [root];
const ans = [];
while (q.length) {
for (let n = q.length; n; --n) {
const { val, left, right } = q.shift();
const t = [];
for (const { val, left, right } of q) {
ans.push(val);
left && q.push(left);
right && q.push(right);
left && t.push(left);
right && t.push(right);
}
q.splice(0, q.length, ...t);
}
return ans;
};
16 changes: 8 additions & 8 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ use std::cell::RefCell;
use std::collections::VecDeque;
impl Solution {
pub fn level_order(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = Vec::new();
let mut queue = VecDeque::new();
let mut ans = Vec::new();
let mut q = VecDeque::new();
if let Some(node) = root {
queue.push_back(node);
q.push_back(node);
}
while let Some(node) = queue.pop_front() {
while let Some(node) = q.pop_front() {
let mut node = node.borrow_mut();
res.push(node.val);
ans.push(node.val);
if let Some(l) = node.left.take() {
queue.push_back(l);
q.push_back(l);
}
if let Some(r) = node.right.take() {
queue.push_back(r);
q.push_back(r);
}
}
res
ans
}
}
23 changes: 13 additions & 10 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
*/

function levelOrder(root: TreeNode | null): number[] {
const res = [];
if (root == null) {
return res;
const ans: number[] = [];
if (!root) {
return ans;
}
const queue = [root];
while (queue.length !== 0) {
const { val, left, right } = queue.shift();
res.push(val);
left && queue.push(left);
right && queue.push(right);
const q: TreeNode[] = [root];
while (q.length) {
const t: TreeNode[] = [];
for (const { val, left, right } of q) {
ans.push(val);
left && t.push(left);
right && t.push(right);
}
q.splice(0, q.length, ...t);
}
return res;
return ans;
}
139 changes: 33 additions & 106 deletions lcof/面试题34. 二叉树中和为某一值的路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,26 @@ func pathSum(root *TreeNode, target int) (ans [][]int) {
*/

function pathSum(root: TreeNode | null, target: number): number[][] {
const res: number[][] = [];
if (root == null) {
return res;
}
const paths: number[] = [];
const dfs = ({ val, right, left }: TreeNode, target: number) => {
paths.push(val);
target -= val;
if (left == null && right == null) {
if (target === 0) {
res.push([...paths]);
}
} else {
left && dfs(left, target);
right && dfs(right, target);
const ans: number[][] = [];
const t: number[] = [];

const dfs = (root: TreeNode | null, s: number): void => {
if (!root) {
return;
}
const { val, left, right } = root;
t.push(val);
s -= val;
if (!left && !right && s === 0) {
ans.push([...t]);
}
paths.pop();
dfs(left, s);
dfs(right, s);
t.pop();
};

dfs(root, target);
return res;
return ans;
}
```

Expand Down Expand Up @@ -278,26 +278,26 @@ impl Solution {
fn dfs(
root: &Option<Rc<RefCell<TreeNode>>>,
mut target: i32,
paths: &mut Vec<i32>,
res: &mut Vec<Vec<i32>>
t: &mut Vec<i32>,
ans: &mut Vec<Vec<i32>>
) {
if let Some(node) = root.as_ref() {
let node = node.borrow();
paths.push(node.val);
t.push(node.val);
target -= node.val;
if node.left.is_none() && node.right.is_none() && target == 0 {
res.push(paths.clone());
ans.push(t.clone());
}
Self::dfs(&node.left, target, paths, res);
Self::dfs(&node.right, target, paths, res);
paths.pop();
Self::dfs(&node.left, target, t, ans);
Self::dfs(&node.right, target, t, ans);
t.pop();
}
}

pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
let mut res = vec![];
Self::dfs(&root, target, &mut vec![], &mut res);
res
let mut ans = vec![];
Self::dfs(&root, target, &mut vec![], &mut ans);
ans
}
}
```
Expand Down Expand Up @@ -325,13 +325,14 @@ var pathSum = function (root, target) {
if (!root) {
return;
}
t.push(root.val);
s -= root.val;
if (!root.left && !root.right && !s) {
const { val, left, right } = root;
t.push(val);
s -= val;
if (!left && !right && !s) {
ans.push([...t]);
}
dfs(root.left, s);
dfs(root.right, s);
dfs(left, s);
dfs(right, s);
t.pop();
};
dfs(root, target);
Expand Down Expand Up @@ -384,78 +385,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start-->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
fn dfs(
root: &Option<Rc<RefCell<TreeNode>>>,
mut target: i32,
paths: &mut Vec<i32>
) -> Vec<Vec<i32>> {
let node = root.as_ref().unwrap().borrow();
paths.push(node.val);
target -= node.val;
let mut res = vec![];
// 确定叶结点身份
if node.left.is_none() && node.right.is_none() {
if target == 0 {
res.push(paths.clone());
}
} else {
if node.left.is_some() {
let res_l = Self::dfs(&node.left, target, paths);
if !res_l.is_empty() {
res = [res, res_l].concat();
}
}
if node.right.is_some() {
let res_r = Self::dfs(&node.right, target, paths);
if !res_r.is_empty() {
res = [res, res_r].concat();
}
}
}
paths.pop();
res
}
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
if root.is_none() {
return vec![];
}
Self::dfs(&root, target, &mut vec![])
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ var pathSum = function (root, target) {
if (!root) {
return;
}
t.push(root.val);
s -= root.val;
if (!root.left && !root.right && !s) {
const { val, left, right } = root;
t.push(val);
s -= val;
if (!left && !right && !s) {
ans.push([...t]);
}
dfs(root.left, s);
dfs(root.right, s);
dfs(left, s);
dfs(right, s);
t.pop();
};
dfs(root, target);
Expand Down
Loading
Loading