Given a board with N positions (1 to N) and a starting position start, some positions have teleporters that move you to a target position (ladders or snakes). Roll a six-sided dice once:
Return a list of all possible final positions after one roll and any teleportation.
Input:
Output:
You are given the root of a binary tree and an integer targetSum. Determine the total number of downward paths where the sum of node values along the path equals targetSum.
Important Notes:
Example:
Binary Tree:
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
targetSum = 8
Output: 3
Explanation:
The three valid paths that sum to 8 are:
You need to check every possible starting node, and from that node, recursively check all downward paths that could sum up to targetSum.
To do this efficiently: Traverse every node in the tree.
At each node, try all downward paths starting from it and count how many of them sum to targetSum. function pathSum(root, targetSum) { if (!root) return 0; // Count paths that start from the current node function countPathsFromNode(node, remainingSum) { if (!node) return 0; let count = 0; if (node.val === remainingSum) count++; count += countPathsFromNode(node.left, remainingSum - node.val); count += countPathsFromNode(node.right, remainingSum - node.val); return count; } // Try all nodes as starting point return ( countPathsFromNode(root, targetSum) + pathSum(root.left, targetSum) + pathSum(root.right, targetSum) ); }

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
What is recursion?