# Just another java solution, recursive. simple but can be simpler.

• ``````/**
* 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) {
//start it off with initial sum of 0, pass in root.
return helper(root,0,sum);
}

public boolean helper(TreeNode node,int currSum,int wanted){
if(node==null)
return false;

//node is a leaf
if(node.left==null && node.right==null){
if((node.val+currSum) == wanted) //check if it sumeed up.
return true;
return false;
}

//not a leaf, so recursively try again but with child nodes, pass in current sum + val of this node.
return (helper(node.left,currSum+node.val,wanted) || helper(node.right,currSum+node.val,wanted));

}
}``````

• how to be simpler?

• This post is deleted!

• This post is deleted!

• This post is deleted!

• Please paste code with some explanation. Thanks.

• Please paste code with some explanation. Thanks.

• Please paste code with some explanation. Thanks.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.