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