-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_sum3.cpp
More file actions
153 lines (134 loc) · 3.07 KB
/
path_sum3.cpp
File metadata and controls
153 lines (134 loc) · 3.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* =====================================================================================
*
* Filename: path_sum3.cpp
*
* Description: Path Sum III.
* You are given a binary tree in which each node contains an integer
* value. Find the number of paths that sum to a given value.
*
* Version: 1.0
* Created: 07/02/18 01:50:44
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <string>
#include <vector>
/**
* Definition for a binary tree node.
*/
struct TreeNode
{
TreeNode(int x) : val(x), left(NULL), right(NULL) { }
int val;
TreeNode *left;
TreeNode *right;
};
class Solution
{
public:
int pathSum(TreeNode* root, int sum)
{
if (root == NULL)
{
return 0;
}
int count = innerSum(root, sum);
if (root->left != NULL)
{
count += pathSum(root->left, sum);
}
if (root->right != NULL)
{
count += pathSum(root->right, sum);
}
return count;
}
private:
int innerSum(TreeNode* root, int sum)
{
int count = 0;
if ((sum - root->val) == 0)
{
count = 1;
}
if (root->left != NULL)
{
count += innerSum(root->left, (sum - root->val));
}
if (root->right != NULL)
{
count += innerSum(root->right, (sum - root->val));
}
return count;
}
};
TreeNode* createTree(const std::vector<std::string>& strs)
{
TreeNode** nodes = new TreeNode*[strs.size()];
for (size_t i = 0; i < strs.size(); i++)
{
nodes[i] = NULL;
}
TreeNode* root = NULL;
for (size_t i = 0; i < strs.size(); i++)
{
if (strs[i] == "null")
{
continue;
}
int val = atoi(strs[i].c_str());
nodes[i] = new TreeNode(val);
int parent = (i + 1) / 2 - 1;
if (parent < 0)
{
continue;
}
else if (((parent + 1) * 2 - 1) == i)
{
nodes[parent]->left = nodes[i];
}
else
{
nodes[parent]->right = nodes[i];
}
}
root = nodes[0];
delete[] nodes;
return root;
}
void deleteTree(TreeNode* root)
{
if (root->left != NULL)
{
deleteTree(root->left);
}
if (root->right != NULL)
{
deleteTree(root->right);
}
delete root;
}
int main(int argc, char* argv[])
{
std::vector<std::string> strs = {"10", "5", "-3", "3", "2", "null", "11", "3", "-2", "null", "1"};
if (argc > 1)
{
strs.clear();
for (int i = 1; i < argc; i++)
{
strs.push_back(argv[i]);
}
}
TreeNode* root = createTree(strs);
int count = Solution().pathSum(root, 8);
printf("count of path: %d\n", count);
deleteTree(root);
return 0;
}