MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / hasPathSum

Method hasPathSum

Java/path-sum.java:17–39  ·  view source on GitHub ↗
(TreeNode root, int sum)

Source from the content-addressed store, hash-verified

15 */
16class Solution {
17 public boolean hasPathSum(TreeNode root, int sum) {
18 if(root == null){
19 // exception
20 return false;
21 }
22 if(root.left == null && root.right == null){
23 if(sum == root.val){
24 return true;
25 }
26 return false;
27 }
28 else{
29 boolean left = false;
30 boolean right = false;
31 if(root.left != null){
32 left = hasPathSum(root.left, sum-root.val);
33 }
34 if(root.right != null){
35 right = hasPathSum(root.right, sum-root.val);
36 }
37 return left||right;
38 }
39 }
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected