-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb99d60
commit bae8e2b
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class PathSum { | ||
/* | ||
Runtime: 0 ms, faster than 100.00% of Java online submissions for Path Sum. | ||
Memory Usage: 39.2 MB, less than 35.65% of Java online submissions for Path Sum. | ||
*/ | ||
public boolean hasPathSum(TreeNode root, int sum) { | ||
if(root==null && sum==0) | ||
return false; | ||
else | ||
return checkSumofPath(root,sum); | ||
} | ||
|
||
public boolean checkSumofPath(TreeNode root, int sum) | ||
{ | ||
if(root==null) | ||
return false; | ||
if (root.left == null && root.right == null) return sum == root.val; | ||
|
||
return checkSumofPath(root.left,sum-root.val)||checkSumofPath(root.right,sum-root.val); | ||
|
||
} | ||
} |