MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0112/Solution.java:14–27  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/11 desc :

Source from the content-addressed store, hash-verified

12 * </pre>
13 */
14public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected