Skip to content

Instantly share code, notes, and snippets.

@Cee
Created May 27, 2014 02:18
Show Gist options
  • Save Cee/a61b7fc5327fc853686f to your computer and use it in GitHub Desktop.
Save Cee/a61b7fc5327fc853686f to your computer and use it in GitHub Desktop.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if ((root.left == null) && (root.right == null)){
if (sum == root.val){
return true;
}else{
return false;
}
}else{
boolean ret = false;
if (root.left != null) ret = ret || hasPathSum(root.left, sum - root.val);
if (root.right != null) ret = ret || hasPathSum(root.right, sum - root.val);
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment