Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
begeekmyfriend committed Nov 7, 2017
2 parents fc46c72 + a184333 commit fd65200
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 129_sum_root_to_leaf_numbers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test sum_tree.c
64 changes: 64 additions & 0 deletions 129_sum_root_to_leaf_numbers/sum_tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>

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

static void recursive(struct TreeNode* node, int sum, int *total)
{
sum = sum * 10 + node->val;

if (node->left == NULL && node->right == NULL) {
*total += sum;
}

if (node->left != NULL) {
recursive(node->left, sum, total);
}

if (node->right != NULL) {
recursive(node->right, sum, total);
}
}

static int sumNumbers(struct TreeNode* root)
{
if (root == NULL) {
return 0;
}

int total = 0;
recursive(root, 0, &total);
return total;
}

int main(void)
{
struct TreeNode root, n10, n11, n20, n21, n22, n23;
root.val = 1;
n10.val = 2;
n11.val = 2;
n20.val = 3;
n21.val = 4;
n22.val = 4;
n23.val = 3;
root.left = &n10;
root.right = &n11;
n10.left = &n20;
n10.right = &n21;
n11.left = &n22;
n11.right = &n23;
n20.left = NULL;
n20.right = NULL;
n21.left = NULL;
n21.right = NULL;
n22.left = NULL;
n22.right = NULL;
n23.left = NULL;
n23.right = NULL;
printf("%d\n", sumNumbers(&root));
return 0;
}

0 comments on commit fd65200

Please sign in to comment.