Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 12, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/3e0207265bb819e9ebad19e3474ae90d to your computer and use it in GitHub Desktop.
Save InterviewBytes/3e0207265bb819e9ebad19e3474ae90d to your computer and use it in GitHub Desktop.
Has path sum
package com.interviewbytes.trees;
public class HasPathSum {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if (root.left == null && root.right == null) return sum == root.val;
final int nextValue = sum - root.val;
return hasPathSum(root.left, nextValue) || hasPathSum(root.right, nextValue);
}
}
package com.interviewbytes.trees;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment