From d7ef7fcd377f9164d1dde715a27c58481be7b3bc Mon Sep 17 00:00:00 2001 From: Leo Ma Date: Sat, 21 Oct 2017 07:32:15 +0800 Subject: [PATCH] Path sum Signed-off-by: Leo Ma --- 112_path_sum/Makefile | 2 + 112_path_sum/path_sum.c | 56 +++++++++++++++++++++++ 113_path_sum_ii/Makefile | 2 + 113_path_sum_ii/path_sum.c | 91 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 112_path_sum/Makefile create mode 100644 112_path_sum/path_sum.c create mode 100644 113_path_sum_ii/Makefile create mode 100644 113_path_sum_ii/path_sum.c diff --git a/112_path_sum/Makefile b/112_path_sum/Makefile new file mode 100644 index 0000000..770d42b --- /dev/null +++ b/112_path_sum/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test path_sum.c diff --git a/112_path_sum/path_sum.c b/112_path_sum/path_sum.c new file mode 100644 index 0000000..6cc3e6b --- /dev/null +++ b/112_path_sum/path_sum.c @@ -0,0 +1,56 @@ +#include +#include +#include + +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; +} diff --git a/113_path_sum_ii/Makefile b/113_path_sum_ii/Makefile new file mode 100644 index 0000000..770d42b --- /dev/null +++ b/113_path_sum_ii/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test path_sum.c diff --git a/113_path_sum_ii/path_sum.c b/113_path_sum_ii/path_sum.c new file mode 100644 index 0000000..9f07251 --- /dev/null +++ b/113_path_sum_ii/path_sum.c @@ -0,0 +1,91 @@ +#include +#include +#include + +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; +}