-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSumIII.cpp
More file actions
28 lines (25 loc) · 840 Bytes
/
Copy pathPathSumIII.cpp
File metadata and controls
28 lines (25 loc) · 840 Bytes
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
#include <iostream>
using namespace std;
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
class Solution {
public:
/* Sol 0
* Two recursive functions. The root of pathSum indicates the real root node.
* To be honest, I didn't write the correct recursive functions at the first time,
* though I know how to solve it. SO..., I think it should be a medium-difficulty problem.
* hhh
*/
int pathSum(TreeNode* root, int sum) {
if(root==nullptr) return 0;
return pathSum2(root,sum) + pathSum(root->left,sum) + pathSum(root->right,sum);
}
int pathSum2(TreeNode *root,int sum){
if(root==nullptr) return 0;
return (root->val==sum) + pathSum2(root->left,sum-root->val) + pathSum2(root->right,sum-root->val);
}
};