Given a binary tree and an integer **k**. The task is to count the number of paths in the tree with the sum of the nodes equals to **k**.

A path can start from any node and end at any node and must be downward only, i.e. they need not be root node and leaf node, and negative numbers can also be there in the tree.

**Examples:**

Input :k = 5 Root of below binary tree: 1 / \ 3 -1 / \ / \ 2 1 4 5 / / \ \ 1 1 2 6Output :No of paths with sum equals to 5 are: 8 3 2 3 1 1 1 3 1 4 1 1 -1 4 1 -1 4 2 5 1 -1 5Input :k = 3 1 / \ 2 -1 / \ / 1 2 3 / \ 2 5Output :No of paths with sum equals to 3 are : 4

**Approach :**

The implementation of printing paths with path sum equal to k is already discussed in this post using vector. However, storing in vector and recursively moving increases both space and time complexity if we just want to store the count of such paths

An efficient implementation of the above problem can be done in using backtracking and unordered maps.

** Steps: **

- We will be using a unordered map which will be filled with various path sum.
- For every node we will check if current sum and root’s value equal to k or not. If the sum equals to k then increment the required answer by one.
- Then we will add all those path sum in map which differs from current
**sum+root->data**value by a constant integer k. - Then we will be inserting the
**current sum + root->data**value in the map. - We will recursively check for
**left**and**right subtrees**of current root - After the right subtree is also traversed we will remove the
**current sum + root->data**value from the map so that it is not taken into consideration in further traversals of other nodes other than the current root’s

Below is the implementation of the above approach:

`// C++ program to count the number ` `// of paths with sum equals to k ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Binary tree node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node *left, *right; ` ` ` `Node(` `int` `x) ` ` ` `{ ` ` ` `data = x; ` ` ` `left = right = NULL; ` ` ` `} ` `}; ` ` ` `// Function to backtrack the tree path and ` `// add each path sum in the unordered map ` `void` `k_paths(Node* root, ` `int` `k, unordered_map<` `int` `, ` `int` `>& p, ` ` ` `int` `sum, ` `int` `&res) ` `{ ` ` ` `// If root is not null ` ` ` `if` `(root) ` ` ` `{ ` ` ` `// If root value and previous sum equal ` ` ` `// to k then increase the count ` ` ` `if` `(sum + root->data == k) ` ` ` `res++; ` ` ` ` ` `// Add those values also which differes ` ` ` `// by the current sum and root data by k ` ` ` `res += p[sum + root->data - k]; ` ` ` ` ` `// Insert the sum + root value in the map ` ` ` `p[sum + root->data]++; ` ` ` ` ` `// Move to left and right trees ` ` ` `k_paths(root->left, k, p, sum + root->data, res); ` ` ` `k_paths(root->right, k, p, sum + root->data, res); ` ` ` ` ` `// remove the sum + root->data value from the ` ` ` `// map if they are n not required further or ` ` ` `// they do no sum up to k in any way ` ` ` `p[sum + root->data]--; ` ` ` `} ` `} ` ` ` `// Function to print the count ` `// of paths with sum equals to k ` `int` `printCount(Node* root, ` `int` `k) ` `{ ` ` ` `// To store the required answer ` ` ` `int` `res = 0; ` ` ` ` ` `// To store the sum ` ` ` `unordered_map<` `int` `, ` `int` `> p; ` ` ` ` ` `// Function call ` ` ` `k_paths(root, k, p, 0, res); ` ` ` ` ` `// Return the required answer ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `Node* root = ` `new` `Node(1); ` ` ` `root->left = ` `new` `Node(2); ` ` ` `root->left->left = ` `new` `Node(1); ` ` ` `root->left->right = ` `new` `Node(2); ` ` ` `root->right = ` `new` `Node(-1); ` ` ` `root->right->left = ` `new` `Node(3); ` ` ` `root->right->left->left = ` `new` `Node(2); ` ` ` `root->right->left->right = ` `new` `Node(5); ` ` ` ` ` `int` `k = 3; ` ` ` `cout << ` `"No of paths with sum equals to "` `<< k ` ` ` `<< ` `" are : "` `<< printCount(root, k) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

No of paths with sum equals to 3 are : 4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count even paths in Binary Tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of exponential paths in a Binary Tree
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print Palindromic Paths of Binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check whether a given binary tree is skewed binary tree or not?
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Count the Number of Binary Search Trees present in a Binary Tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Create a mirror tree from the given binary tree
- Check if the given n-ary tree is a binary tree
- Convert a given Binary tree to a tree that holds Logical OR property
- Difference between General tree and Binary tree
- Difference between Binary tree and B-tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.