Skip to content

Commit

Permalink
Create PathSum.java
Browse files Browse the repository at this point in the history
  • Loading branch information
hiteshbhavsar authored Feb 6, 2021
1 parent fb99d60 commit bae8e2b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions PathSum.java
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);

}
}

0 comments on commit bae8e2b

Please sign in to comment.