-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath Sum.cpp
More file actions
31 lines (29 loc) · 715 Bytes
/
Path Sum.cpp
File metadata and controls
31 lines (29 loc) · 715 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
29
30
31
#include<iostream>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
void cur(TreeNode* root,int sum,int tsum,bool& re)
{
tsum+=root->val;
if (root->left==NULL&&root->right==NULL)
{
if (tsum==sum)
re=true;
return;
}
if (root->left!=NULL)
cur(root->left,sum,tsum,re);
if (root->right!=NULL)
cur(root->right,sum,tsum,re);
}
bool hasPathSum(TreeNode* root, int sum) {
bool re=false;
if (root==NULL)
return re;
cur(root,sum,0,re);
return re;
}