author: Blankj blog : http://blankj.com time : 2017/10/11 desc :
| 12 | * </pre> |
| 13 | */ |
| 14 | public class Solution { |
| 15 | public boolean hasPathSum(TreeNode root, int sum) { |
| 16 | if (root == null) return false; |
| 17 | if (root.left == null && root.right == null) return sum == root.val; |
| 18 | return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); |
| 19 | } |
| 20 | |
| 21 | public static void main(String[] args) { |
| 22 | Solution solution = new Solution(); |
| 23 | TreeNode testData = TreeNode.createTestData("[5,4,8,11,null,13,4,7,2,null,null,null,1]"); |
| 24 | TreeNode.print(testData); |
| 25 | System.out.println(solution.hasPathSum(testData, 22)); |
| 26 | } |
| 27 | } |
nothing calls this directly
no outgoing calls
no test coverage detected