Print all k | Java 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
import java . util . * ; class GFG { static void printVector ( Vector < Integer > v , int i ) { for ( int j = i ; j < v . size ( ) ; j ++ ) System . out . print ( v . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } static class Node { int data ; Node left , right ; Node ( int x ) { data = x ; left = right = null ; } } ; static Vector < Integer > path = new Vector < Integer > ( ) ; static void printKPathUtil ( Node root , int k ) { if ( root == null ) return ; path . add ( root . data ) ; printKPathUtil ( root . left , k ) ; printKPathUtil ( root . right , k ) ; int f = 0 ; for ( int j = path . size ( ) - 1 ; j >= 0 ; j -- ) { f += path . get ( j ) ; if ( f == k ) printVector ( path , j ) ; } path . remove ( path . size ( ) - 1 ) ; } static void printKPath ( Node root , int k ) { path = new Vector < Integer > ( ) ; printKPathUtil ( root , k ) ; } public static void main ( String args [ ] ) { 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 ) ; } }