Skip to content

Commit d7ef7fc

Browse files
Path sum
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent abcf598 commit d7ef7fc

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

112_path_sum/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test path_sum.c

112_path_sum/path_sum.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct TreeNode {
6+
int val;
7+
struct TreeNode *left;
8+
struct TreeNode *right;
9+
};
10+
11+
static bool hasPathSum(struct TreeNode *root, int sum)
12+
{
13+
if (root == NULL) {
14+
return false;
15+
} else if (root->left == NULL && root->right == NULL && root->val == sum) {
16+
return true;
17+
} else {
18+
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
19+
}
20+
}
21+
22+
int main(int argc, char **argv)
23+
{
24+
struct TreeNode root, n1[2], n2[4], n3[8];
25+
root.val = 5;
26+
n1[0].val = 4;
27+
n1[1].val = 8;
28+
n2[0].val = 11;
29+
n2[2].val = 13;
30+
n2[3].val = 4;
31+
n3[0].val = 7;
32+
n3[1].val = 2;
33+
n3[7].val = 1;
34+
35+
root.left = &n1[0];
36+
root.right = &n1[1];
37+
n1[0].left = &n2[0];
38+
n1[0].right = NULL;
39+
n1[1].left = &n2[2];
40+
n1[1].right = &n2[3];
41+
n2[0].left = &n3[0];
42+
n2[0].right = &n3[1];
43+
n2[2].left = NULL;
44+
n2[2].right = NULL;
45+
n2[3].left = NULL;
46+
n2[3].right = &n3[7];
47+
n3[0].left = NULL;
48+
n3[0].right = NULL;
49+
n3[1].left = NULL;
50+
n3[1].right = NULL;
51+
n3[7].left = NULL;
52+
n3[7].right = NULL;
53+
54+
printf("%s\n", hasPathSum(&root, 22) ? "true" : "false");
55+
return 0;
56+
}

113_path_sum_ii/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test path_sum.c

113_path_sum_ii/path_sum.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
struct TreeNode {
6+
int val;
7+
struct TreeNode *left;
8+
struct TreeNode *right;
9+
};
10+
11+
static void recursive(struct TreeNode *node, int sum, int *stack, int len, int **results, int *sizes, int *count)
12+
{
13+
if (node == NULL) {
14+
return;
15+
}
16+
17+
sum -= node->val;
18+
if (node->left == NULL && node->right == NULL && sum == 0) {
19+
results[*count] = malloc((len + 1) * sizeof(int));
20+
memcpy(results[*count], stack, len * sizeof(int));
21+
results[*count][len] = node->val;
22+
sizes[*count] = len + 1;
23+
(*count)++;
24+
return;
25+
}
26+
stack[len] = node->val;
27+
recursive(node->left, sum, stack, len + 1, results, sizes, count);
28+
recursive(node->right, sum, stack, len + 1, results, sizes, count);
29+
}
30+
31+
static int **pathSum(struct TreeNode *root, int sum, int **columnSizes, int *returnSize)
32+
{
33+
if (root == NULL) {
34+
*returnSize = 0;
35+
return NULL;
36+
}
37+
38+
int level = 5000, cap = 1000;
39+
int *stack = malloc(level * sizeof(int));
40+
int **results = malloc(cap * sizeof(int *));
41+
*columnSizes = malloc(cap * sizeof(int));
42+
recursive(root, sum, stack, 0, results, *columnSizes, returnSize);
43+
return results;
44+
}
45+
46+
int main(int argc, char **argv)
47+
{
48+
struct TreeNode root, n1[2], n2[4], n3[8];
49+
root.val = 5;
50+
n1[0].val = 4;
51+
n1[1].val = 8;
52+
n2[0].val = 11;
53+
n2[2].val = 13;
54+
n2[3].val = 4;
55+
n3[0].val = 7;
56+
n3[1].val = 2;
57+
n3[6].val = 5;
58+
n3[7].val = 1;
59+
60+
root.left = &n1[0];
61+
root.right = &n1[1];
62+
n1[0].left = &n2[0];
63+
n1[0].right = NULL;
64+
n1[1].left = &n2[2];
65+
n1[1].right = &n2[3];
66+
n2[0].left = &n3[0];
67+
n2[0].right = &n3[1];
68+
n2[2].left = NULL;
69+
n2[2].right = NULL;
70+
n2[3].left = &n3[6];
71+
n2[3].right = &n3[7];
72+
n3[0].left = NULL;
73+
n3[0].right = NULL;
74+
n3[1].left = NULL;
75+
n3[1].right = NULL;
76+
n3[6].left = NULL;
77+
n3[6].right = NULL;
78+
n3[7].left = NULL;
79+
n3[7].right = NULL;
80+
81+
int i, j, count = 0;
82+
int *sizes;
83+
int **list = pathSum(&root, 22, &sizes, &count);
84+
for (i = 0; i < count; i++) {
85+
for (j = 0; j < sizes[i]; j++) {
86+
printf("%d ", list[i][j]);
87+
}
88+
printf("\n");
89+
}
90+
return 0;
91+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy