Skip to content

Commit

Permalink
Path sum
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 20, 2017
1 parent abcf598 commit d7ef7fc
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 112_path_sum/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test path_sum.c
56 changes: 56 additions & 0 deletions 112_path_sum/path_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

static bool hasPathSum(struct TreeNode *root, int sum)
{
if (root == NULL) {
return false;
} else if (root->left == NULL && root->right == NULL && root->val == sum) {
return true;
} else {
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
}

int main(int argc, char **argv)
{
struct TreeNode root, n1[2], n2[4], n3[8];
root.val = 5;
n1[0].val = 4;
n1[1].val = 8;
n2[0].val = 11;
n2[2].val = 13;
n2[3].val = 4;
n3[0].val = 7;
n3[1].val = 2;
n3[7].val = 1;

root.left = &n1[0];
root.right = &n1[1];
n1[0].left = &n2[0];
n1[0].right = NULL;
n1[1].left = &n2[2];
n1[1].right = &n2[3];
n2[0].left = &n3[0];
n2[0].right = &n3[1];
n2[2].left = NULL;
n2[2].right = NULL;
n2[3].left = NULL;
n2[3].right = &n3[7];
n3[0].left = NULL;
n3[0].right = NULL;
n3[1].left = NULL;
n3[1].right = NULL;
n3[7].left = NULL;
n3[7].right = NULL;

printf("%s\n", hasPathSum(&root, 22) ? "true" : "false");
return 0;
}
2 changes: 2 additions & 0 deletions 113_path_sum_ii/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test path_sum.c
91 changes: 91 additions & 0 deletions 113_path_sum_ii/path_sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

static void recursive(struct TreeNode *node, int sum, int *stack, int len, int **results, int *sizes, int *count)
{
if (node == NULL) {
return;
}

sum -= node->val;
if (node->left == NULL && node->right == NULL && sum == 0) {
results[*count] = malloc((len + 1) * sizeof(int));
memcpy(results[*count], stack, len * sizeof(int));
results[*count][len] = node->val;
sizes[*count] = len + 1;
(*count)++;
return;
}
stack[len] = node->val;
recursive(node->left, sum, stack, len + 1, results, sizes, count);
recursive(node->right, sum, stack, len + 1, results, sizes, count);
}

static int **pathSum(struct TreeNode *root, int sum, int **columnSizes, int *returnSize)
{
if (root == NULL) {
*returnSize = 0;
return NULL;
}

int level = 5000, cap = 1000;
int *stack = malloc(level * sizeof(int));
int **results = malloc(cap * sizeof(int *));
*columnSizes = malloc(cap * sizeof(int));
recursive(root, sum, stack, 0, results, *columnSizes, returnSize);
return results;
}

int main(int argc, char **argv)
{
struct TreeNode root, n1[2], n2[4], n3[8];
root.val = 5;
n1[0].val = 4;
n1[1].val = 8;
n2[0].val = 11;
n2[2].val = 13;
n2[3].val = 4;
n3[0].val = 7;
n3[1].val = 2;
n3[6].val = 5;
n3[7].val = 1;

root.left = &n1[0];
root.right = &n1[1];
n1[0].left = &n2[0];
n1[0].right = NULL;
n1[1].left = &n2[2];
n1[1].right = &n2[3];
n2[0].left = &n3[0];
n2[0].right = &n3[1];
n2[2].left = NULL;
n2[2].right = NULL;
n2[3].left = &n3[6];
n2[3].right = &n3[7];
n3[0].left = NULL;
n3[0].right = NULL;
n3[1].left = NULL;
n3[1].right = NULL;
n3[6].left = NULL;
n3[6].right = NULL;
n3[7].left = NULL;
n3[7].right = NULL;

int i, j, count = 0;
int *sizes;
int **list = pathSum(&root, 22, &sizes, &count);
for (i = 0; i < count; i++) {
for (j = 0; j < sizes[i]; j++) {
printf("%d ", list[i][j]);
}
printf("\n");
}
return 0;
}

0 comments on commit d7ef7fc

Please sign in to comment.