Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver Code ; Desigining the tree ; Counter counts no . of consecutive nodes
let adj = new Array ( 100005 ) ; let visited = new Array ( 100005 ) ; visited . fill ( 0 ) ; let ans = 0 ; function dfs ( node , count , m , arr , k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . length == 1 && node != 1 ) { ans ++ ; } for ( let x = 0 ; x < adj [ node ] . length ; x ++ ) { if ( visited [ adj [ node ] [ x ] ] == 0 ) { dfs ( adj [ node ] [ x ] , count , m , arr , k ) ; } } } let arr = [ 2 , 1 , 3 , 2 , 1 , 2 , 1 ] ; let K = 2 , M = 2 ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; adj [ 1 ] . push ( 2 ) ; adj [ 2 ] . push ( 1 ) ; adj [ 1 ] . push ( 3 ) ; adj [ 3 ] . push ( 1 ) ; adj [ 2 ] . push ( 4 ) ; adj [ 4 ] . push ( 2 ) ; adj [ 2 ] . push ( 5 ) ; adj [ 5 ] . push ( 2 ) ; adj [ 3 ] . push ( 6 ) ; adj [ 6 ] . push ( 3 ) ; adj [ 3 ] . push ( 7 ) ; adj [ 7 ] . push ( 3 ) ; let counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; document . write ( ans + " " ) ;