Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const hasPathToSum = function(node, targetSum) {
- var wasFound = false;
- var getBranchSum = function(node, sum = 0) {
- sum += node.value;
- if (node.left) {
- getBranchSum(node.left, sum);
- }
- if (node.right) {
- getBranchSum(node.right, sum);
- }
- if (!node.left && !node.right) {
- if (sum === targetSum) {
- wasFound = true;
- }
- }
- };
- getBranchSum(node);
- return wasFound;
- };
- const hasPathToSum = function(node, targetSum) {
- var allBranchSums = [];
- var getBranchSum = (node, sum=0) => {
- sum += node.value;
- if (node.left) {
- getBranchSum(node.left, sum);
- }
- if (node.right) {
- getBranchSum(node.right, sum);
- }
- if (!node.left && !node.right) {
- allBranchSums.push(sum);
- }
- };
- getBranchSum(node);
- return allBranchSums.includes(targetSum);
- };
Add Comment
Please, Sign In to add comment