Created
May 27, 2014 02:18
-
-
Save Cee/a61b7fc5327fc853686f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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