Skip to content

Commit 6af86ee

Browse files
Oct 4th : Path sum
1 parent 300301b commit 6af86ee

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Trees/pathSum.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
struct TreeNode{
5+
int val;
6+
TreeNode *left,*right;
7+
};
8+
bool hasPathSum(TreeNode* root, int sum) {
9+
if(!root)
10+
return 0;
11+
12+
if(!root->left && !root->right)
13+
return sum-root->val == 0;
14+
return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
15+
}

0 commit comments

Comments
 (0)