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 87b316a

Browse files
committed
feat: add solutions to lc problems: No.0589, 0799
- No.0589.N-ary Tree Preorder Traversal - No.0799.Champagne Tower
1 parent 1efa4f8 commit 87b316a

File tree

8 files changed

+258
-31
lines changed

8 files changed

+258
-31
lines changed

‎solution/0500-0599/0589.N-ary Tree Preorder Traversal/README.md‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,52 @@ function preorder(root: Node | null): number[] {
241241
*/
242242

243243
function preorder(root: Node | null): number[] {
244-
if (root == null) {
245-
return [];
244+
const ans = [];
245+
const dfs = (root: Node | null) => {
246+
if (root == null) {
247+
return;
248+
}
249+
ans.push(root.val);
250+
for (const node of root.children) {
251+
dfs(node);
252+
}
253+
};
254+
dfs(root);
255+
return ans;
256+
}
257+
```
258+
259+
### **C**
260+
261+
```c
262+
/**
263+
* Definition for a Node.
264+
* struct Node {
265+
* int val;
266+
* int numChildren;
267+
* struct Node** children;
268+
* };
269+
*/
270+
271+
/**
272+
* Note: The returned array must be malloced, assume caller calls free().
273+
*/
274+
275+
void dfs(struct Node *root, int *ans, int *i) {
276+
if (!root) {
277+
return;
278+
}
279+
ans[(*i)++] = root->val;
280+
for (int j = 0; j < root->numChildren; j++) {
281+
dfs(root->children[j], ans, i);
246282
}
247-
const { val, children } =root;
248-
return [
249-
val,
250-
...children
251-
.map(node=>preorder(node))
252-
.reduce((p, v) =>p.concat(v), []),
253-
];
283+
}
284+
285+
int *preorder(struct Node *root, int *returnSize) {
286+
int *ans = malloc(sizeof(int) * 10000);
287+
*returnSize = 0;
288+
dfs(root, ans, returnSize);
289+
return ans;
254290
}
255291
```
256292

‎solution/0500-0599/0589.N-ary Tree Preorder Traversal/README_EN.md‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,52 @@ function preorder(root: Node | null): number[] {
231231
*/
232232

233233
function preorder(root: Node | null): number[] {
234-
if (root == null) {
235-
return [];
234+
const ans = [];
235+
const dfs = (root: Node | null) => {
236+
if (root == null) {
237+
return;
238+
}
239+
ans.push(root.val);
240+
for (const node of root.children) {
241+
dfs(node);
242+
}
243+
};
244+
dfs(root);
245+
return ans;
246+
}
247+
```
248+
249+
### **C**
250+
251+
```c
252+
/**
253+
* Definition for a Node.
254+
* struct Node {
255+
* int val;
256+
* int numChildren;
257+
* struct Node** children;
258+
* };
259+
*/
260+
261+
/**
262+
* Note: The returned array must be malloced, assume caller calls free().
263+
*/
264+
265+
void dfs(struct Node *root, int *ans, int *i) {
266+
if (!root) {
267+
return;
268+
}
269+
ans[(*i)++] = root->val;
270+
for (int j = 0; j < root->numChildren; j++) {
271+
dfs(root->children[j], ans, i);
236272
}
237-
const { val, children } =root;
238-
return [
239-
val,
240-
...children
241-
.map(node=>preorder(node))
242-
.reduce((p, v) =>p.concat(v), []),
243-
];
273+
}
274+
275+
int *preorder(struct Node *root, int *returnSize) {
276+
int *ans = malloc(sizeof(int) * 10000);
277+
*returnSize = 0;
278+
dfs(root, ans, returnSize);
279+
return ans;
244280
}
245281
```
246282
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a Node.
3+
* struct Node {
4+
* int val;
5+
* int numChildren;
6+
* struct Node** children;
7+
* };
8+
*/
9+
10+
/**
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
14+
void dfs(struct Node *root, int *ans, int *i) {
15+
if (!root) {
16+
return;
17+
}
18+
ans[(*i)++] = root->val;
19+
for (int j = 0; j < root->numChildren; j++) {
20+
dfs(root->children[j], ans, i);
21+
}
22+
}
23+
24+
int *preorder(struct Node *root, int *returnSize) {
25+
int *ans = malloc(sizeof(int) * 10000);
26+
*returnSize = 0;
27+
dfs(root, ans, returnSize);
28+
return ans;
29+
}

‎solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111
*/
1212

1313
function preorder(root: Node | null): number[] {
14-
const res = [];
15-
if (root == null) {
16-
return res;
17-
}
18-
const stack = [root];
19-
while (stack.length !== 0) {
20-
const { val, children } = stack.pop();
21-
res.push(val);
22-
const n = children.length;
23-
for (let i = n - 1; i >= 0; i--) {
24-
stack.push(children[i]);
14+
const ans = [];
15+
const dfs = (root: Node | null) => {
16+
if (root == null) {
17+
return;
2518
}
26-
}
27-
return res;
19+
ans.push(root.val);
20+
for (const node of root.children) {
21+
dfs(node);
22+
}
23+
};
24+
dfs(root);
25+
return ans;
2826
}

‎solution/0700-0799/0799.Champagne Tower/README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,52 @@ func champagneTower(poured int, query_row int, query_glass int) float64 {
229229
}
230230
```
231231

232+
### **TypeScipt**
233+
234+
```ts
235+
function champagneTower(
236+
poured: number,
237+
query_row: number,
238+
query_glass: number,
239+
): number {
240+
let row = [poured];
241+
for (let i = 1; i <= query_row; i++) {
242+
const nextRow = new Array(i + 1).fill(0);
243+
for (let j = 0; j < i; j++) {
244+
if (row[j] > 1) {
245+
nextRow[j] += (row[j] - 1) / 2;
246+
nextRow[j + 1] += (row[j] - 1) / 2;
247+
}
248+
}
249+
row = nextRow;
250+
}
251+
return Math.min(1, row[query_glass]);
252+
}
253+
```
254+
255+
### **Rust**
256+
257+
```rust
258+
impl Solution {
259+
pub fn champagne_tower(poured: i32, query_row: i32, query_glass: i32) -> f64 {
260+
let query_row = query_row as usize;
261+
let query_glass = query_glass as usize;
262+
let mut row = vec![poured as f64];
263+
for i in 1..=query_row {
264+
let mut next_row = vec![0f64; i + 1];
265+
for j in 0..i {
266+
if row[j] > 1f64 {
267+
next_row[j] += (row[j] - 1f64) / 2f64;
268+
next_row[j + 1] += (row[j] - 1f64) / 2f64;
269+
}
270+
}
271+
row = next_row;
272+
}
273+
1f64.min(row[query_glass])
274+
}
275+
}
276+
```
277+
232278
### **...**
233279

234280
```

‎solution/0700-0799/0799.Champagne Tower/README_EN.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,52 @@ func champagneTower(poured int, query_row int, query_glass int) float64 {
220220
}
221221
```
222222

223+
### **TypeScipt**
224+
225+
```ts
226+
function champagneTower(
227+
poured: number,
228+
query_row: number,
229+
query_glass: number,
230+
): number {
231+
let row = [poured];
232+
for (let i = 1; i <= query_row; i++) {
233+
const nextRow = new Array(i + 1).fill(0);
234+
for (let j = 0; j < i; j++) {
235+
if (row[j] > 1) {
236+
nextRow[j] += (row[j] - 1) / 2;
237+
nextRow[j + 1] += (row[j] - 1) / 2;
238+
}
239+
}
240+
row = nextRow;
241+
}
242+
return Math.min(1, row[query_glass]);
243+
}
244+
```
245+
246+
### **Rust**
247+
248+
```rust
249+
impl Solution {
250+
pub fn champagne_tower(poured: i32, query_row: i32, query_glass: i32) -> f64 {
251+
let query_row = query_row as usize;
252+
let query_glass = query_glass as usize;
253+
let mut row = vec![poured as f64];
254+
for i in 1..=query_row {
255+
let mut next_row = vec![0f64; i + 1];
256+
for j in 0..i {
257+
if row[j] > 1f64 {
258+
next_row[j] += (row[j] - 1f64) / 2f64;
259+
next_row[j + 1] += (row[j] - 1f64) / 2f64;
260+
}
261+
}
262+
row = next_row;
263+
}
264+
1f64.min(row[query_glass])
265+
}
266+
}
267+
```
268+
223269
### **...**
224270

225271
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn champagne_tower(poured: i32, query_row: i32, query_glass: i32) -> f64 {
3+
let query_row = query_row as usize;
4+
let query_glass = query_glass as usize;
5+
let mut row = vec![poured as f64];
6+
for i in 1..=query_row {
7+
let mut next_row = vec![0f64; i + 1];
8+
for j in 0..i {
9+
if row[j] > 1f64 {
10+
next_row[j] += (row[j] - 1f64) / 2f64;
11+
next_row[j + 1] += (row[j] - 1f64) / 2f64;
12+
}
13+
}
14+
row = next_row;
15+
}
16+
1f64.min(row[query_glass])
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function champagneTower(
2+
poured: number,
3+
query_row: number,
4+
query_glass: number,
5+
): number {
6+
let row = [poured];
7+
for (let i = 1; i <= query_row; i++) {
8+
const nextRow = new Array(i + 1).fill(0);
9+
for (let j = 0; j < i; j++) {
10+
if (row[j] > 1) {
11+
nextRow[j] += (row[j] - 1) / 2;
12+
nextRow[j + 1] += (row[j] - 1) / 2;
13+
}
14+
}
15+
row = nextRow;
16+
}
17+
return Math.min(1, row[query_glass]);
18+
}

0 commit comments

Comments
(0)

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