A binary tree and a number k are given. Print every path in the tree with sum of the nodes in the path as 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 6 Output : 3 2 3 1 1 1 3 1 4 1 1 -1 4 1 -1 4 2 5 1 -1 5

Source : Amazon Interview Experience Set-323

Kindly note that this problem is significantly different from finding k-sum path from root to leaves. Here each node can be treated as root, hence the path can start and end at any node.

The basic idea to solve the problem is to do a preorder traversal of the given tree. We also need a container (vector) to keep track of the path that led to that node. At each node we check if there are any path that sums to k, if any we print the path and proceed recursively to print each path.

Below is the C++ program for the same.

`// C++ program to print all paths with sum k. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `//utility function to print contents of ` `//a vector from index i to it's end ` `void` `printVector(` `const` `vector<` `int` `>& v, ` `int` `i) ` `{ ` ` ` `for` `(` `int` `j=i; j<v.size(); j++) ` ` ` `cout << v[j] << ` `" "` `; ` ` ` `cout << endl; ` `} ` ` ` `// binary tree node ` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `Node *left,*right; ` ` ` `Node(` `int` `x) ` ` ` `{ ` ` ` `data = x; ` ` ` `left = right = NULL; ` ` ` `} ` `}; ` ` ` `// This function prints all paths that have sum k ` `void` `printKPathUtil(Node *root, vector<` `int` `>& path, ` ` ` `int` `k) ` `{ ` ` ` `// empty node ` ` ` `if` `(!root) ` ` ` `return` `; ` ` ` ` ` `// add current node to the path ` ` ` `path.push_back(root->data); ` ` ` ` ` `// check if there's any k sum path ` ` ` `// in the left sub-tree. ` ` ` `printKPathUtil(root->left, path, k); ` ` ` ` ` `// check if there's any k sum path ` ` ` `// in the right sub-tree. ` ` ` `printKPathUtil(root->right, path, k); ` ` ` ` ` `// check if there's any k sum path that ` ` ` `// terminates at this node ` ` ` `// Traverse the entire path as ` ` ` `// there can be negative elements too ` ` ` `int` `f = 0; ` ` ` `for` `(` `int` `j=path.size()-1; j>=0; j--) ` ` ` `{ ` ` ` `f += path[j]; ` ` ` ` ` `// If path sum is k, print the path ` ` ` `if` `(f == k) ` ` ` `printVector(path, j); ` ` ` `} ` ` ` ` ` `// Remove the current element from the path ` ` ` `path.pop_back(); ` `} ` ` ` `// A wrapper over printKPathUtil() ` `void` `printKPath(Node *root, ` `int` `k) ` `{ ` ` ` `vector<` `int` `> path; ` ` ` `printKPathUtil(root, path, k); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `Node *root = ` `new` `Node(1); ` ` ` `root->left = ` `new` `Node(3); ` ` ` `root->left->left = ` `new` `Node(2); ` ` ` `root->left->right = ` `new` `Node(1); ` ` ` `root->left->right->left = ` `new` `Node(1); ` ` ` `root->right = ` `new` `Node(-1); ` ` ` `root->right->left = ` `new` `Node(4); ` ` ` `root->right->left->left = ` `new` `Node(1); ` ` ` `root->right->left->right = ` `new` `Node(2); ` ` ` `root->right->right = ` `new` `Node(5); ` ` ` `root->right->right->right = ` `new` `Node(2); ` ` ` ` ` `int` `k = 5; ` ` ` `printKPath(root, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

3 2 3 1 1 1 3 1 4 1 1 -1 4 1 -1 4 2 5 1 -1 5

This article is contributed by **Ashutosh Kumar** 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Print Binary Tree in 2-Dimensions
- Print Right View of a Binary Tree
- Print all even nodes of Binary Search Tree
- Print a Binary Tree in Vertical Order | Set 1
- Print cousins of a given node in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print nodes in the Top View of Binary Tree | Set 3
- Print Nodes in Top View of Binary Tree
- Print all full nodes in a Binary Tree
- Print all nodes in a binary tree having K leaves