Knowee
Questions
Features
Study Tools

Given a binary tree and an integer K. Find the number of paths in the tree which have their sum equal to K.A path may start from any node and end at any node in the downward direction.Since the answer may be very large, compute it modulo 109+7.

Question

Given a binary tree and an integer K. Find the number of paths in the tree which have their sum equal to K.A path may start from any node and end at any node in the downward direction.Since the answer may be very large, compute it modulo 109+7.

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using Depth-First Search (DFS) and prefix sum. Here are the steps:

  1. Initialize a global variable count to keep track of the number of paths that sum to K.

  2. Create a recursive function dfs(node, prefixSum). This function will perform a depth-first search on the binary tree, keeping track of the sum of the path from the root to the current node.

  3. In the dfs function, if the node is null, return.

  4. Add the value of the current node to prefixSum.

  5. Check if there are any paths that end at the current node and sum to K. This can be done by checking if prefixSum - K exists in the prefix sum map. If it does, add the value of prefixSum - K in the map to count.

  6. Add prefixSum to the prefix sum map.

  7. Recursively call dfs on the left and right children of the current node.

  8. After visiting all children of the current node, remove prefixSum from the map to backtrack.

  9. After the DFS is done, return count modulo 109+7.

Here is a Python code snippet that implements these steps:

from collections import defaultdict

MOD = 10**9 + 7
count = 0
prefixSumCount = defaultdict(int)

def dfs(node, prefixSum):
    global count
    if not node:
        return
    prefixSum += node.val
    count += prefixSumCount[prefixSum - K]
    prefixSumCount[prefixSum] += 1
    dfs(node.left, prefixSum)
    dfs(node.right, prefixSum)
    prefixSumCount[prefixSum] -= 1

dfs(root, 0)
return count % MOD

In this code, root is the root of the binary tree and K is the target sum. The dfs function is called initially with the root of the tree and a prefix sum of 0.

This problem has been solved

Similar Questions

Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).

Find a highest sumGiven a binary tree in which each node element contains a number. Find the maximum possible path sum from one special node to another special node.Note: Here special node is a node that is connected to exactly one different node.Constraints:2  ≤  Number of nodes  ≤  104-103  ≤ Value of each node ≤ 103Sample input3 4 5 -10 4Sample output16

Question1Max. score: 10.00Find a highest sumGiven a binary tree in which each node element contains a number. Find the maximum possible path sum from one special node to another special node.Note: Here speci

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.A leaf is a node with no children. Example 1:Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22Output: trueExplanation: The root-to-leaf path with the target sum is shown.Example 2:Input: root = [1,2,3], targetSum = 5Output: falseExplanation: There two root-to-leaf paths in the tree:(1 --> 2): The sum is 3.(1 --> 3): The sum is 4.There is no root-to-leaf path with sum = 5.Example 3:Input: root = [], targetSum = 0Output: falseExplanation: Since the tree is empty, there are no root-to-leaf paths. Constraints:The number of nodes in the tree is in the range [0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000

A binary search tree is generated by inserting following integers: 47, 20, 65, 6, 11, 63, 91, 10, 15, 42, 69, 39The number of nodes in the left subtree and right subtree of the root respectively.A7,5B7, 4C4, 7D5,7

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.