Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const hasPathToSum = (node, targetSum) => {
- console.log(node, targetSum);
- // let currentNode = node
- targetSum = targetSum - node.value;
- console.log('Target Sum: ', targetSum)
- if (node.left) {
- console.log("left", targetSum)
- hasPathToSum(node.left, targetSum);
- }
- if (node.right) {
- console.log("right", targetSum)
- hasPathToSum(node.right, targetSum);
- }
- if (targetSum = 0) {
- console.log('FOUND SUM NODE', node)
- return node;
- }
- return targetSum;
- };
- let tree = {
- value: 5,
- left: {
- value: 3,
- left: {
- value: 1,
- left: null,
- right: null
- },
- right: {
- value: 2,
- left: null,
- right: null
- }
- },
- right: {
- value: 7,
- left: null,
- right: null
- }
- }
- let targetSum = 11;
- hasPathToSum(tree, targetSum);
Add Comment
Please, Sign In to add comment