50221. Pascal's triangle

難度:4.5/5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include "Pascal.h"

// typedef struct node {
// int value;
// struct node *left;
// struct node *right;
// } Node;

void dfs(Node* n){
if(n == NULL) return;
n->value += 1;
dfs(n->left);
dfs(n->right);
}

void build_Pascal(Node* arr, int height){
int n = (height) * (height + 1) / 2;
for(int i = 0; i < n; i++) arr[i].value = 0;

// right ptrs
for(int i = 0; i < n - 1; i++){
arr[i].right = &arr[i + 1];
}

// left ptrs
int cnt = 0;
int d = height;
for(int i = 0; i < n - 1; i++){
if(i + d < n) arr[i].left = &arr[i + d];
cnt++;
if(cnt == d) cnt = 0, d--;
}

// remove leaves
int now = -1;
for(int i = height; i > 0; i--){
now += i;
arr[now].right = NULL;
arr[now].left = NULL;
}

dfs(&arr[0]);
}

50221. Pascal's triangle
https://aaronlin1229.github.io/judgegirl_50221/
Author
Akizumi
Posted on
July 17, 2023
Licensed under