Home >Web Front-end >JS Tutorial >Algo:: Tree Sum Should Match the Target
LeetCode 112. Path Sum easy problem .
Question
Solution
var hasPathSum = function(root, targetSum) { let sum = 0; const helper = (root) => { if (root === null) { return; } sum += root.val; if (sum === targetSum && (root.left == null && root.right === null)) { return true; } if (helper(root.left)){ return true; } if (helper(root.right)) { return true; }; sum -= root.val; } return helper(root) ? true : false; };
If it is not clear please check my other Article on Tree Algorithm then it will be very much easier to understand.
Feel Free to reach out to me if you have any concerns.
Reference:-
The above is the detailed content of Algo:: Tree Sum Should Match the Target. For more information, please follow other related articles on the PHP Chinese website!