-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathSum.c
68 lines (52 loc) · 1.56 KB
/
pathSum.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdbool.h>
// 路径之和1
// Definition for a binary tree node.
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
bool dfs(struct TreeNode *root, int targetSum) {
if (root == NULL) return false;
targetSum = targetSum - root->val;
if (root->left == NULL && root->right == NULL && targetSum == 0) {
return true;
}
return dfs(root->left, targetSum) || dfs(root->right, targetSum);
}
bool hasPathSum(struct TreeNode* root, int targetSum){
if (root == NULL) return false;
return dfs(root, targetSum);
}
// 路径之和2
// https://cloud.tencent.com/developer/article/1756156
int **ret;
int retSize;
int *retColSize;
int *path;
int pathSize;
void dfs(struct TreeNode* root, int target) {
if (root == NULL) return;
path[pathSize++] = root->val;
target = target - root->val;
if (root->left == NULL && root->right == NULL && target == 0) {
int *tmp = malloc(sizeof(int) * pathSize);
memcpy(tmp, path, sizeof(int) * pathSize);
ret[retSize] = tmp;
retColSize[retSize++] = pathSize;
}
dfs(root->left, target);
dfs(root->right, target);
pathSize--;
}
int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** returnColumnSizes){
ret = malloc(sizeof(int *) * 2001);
path = malloc(sizeof(int) * 2001);
retColSize = malloc(sizeof(int) * 2001);
retSize = pathSize = 0;
dfs(root, targetSum);
*returnSize = retSize;
*returnColumnSizes = retColSize;
return ret;
}