Find sum of all nodes of the given perfect binary tree | function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; list of vector to store nodes of all of the levels ; initialize ; store the nodes of last level i . e . , the leaf nodes ; store nodes of rest of the level by moving in bottom - up manner ; loop to calculate values of parent nodes from the children nodes of lower level ; store the value of parent node as sum of children nodes ; traverse the list of vector and calculate the sum ; Driver Code
function sumNodes ( l ) { let leafNodeCount = Math . pow ( 2 , l - 1 ) ; let vec = [ ] ; for ( let i = 1 ; i <= l ; i ++ ) { vec . push ( [ ] ) ; } for ( let i = 1 ; i <= leafNodeCount ; i ++ ) { vec [ l - 1 ] . push ( i ) ; } for ( let i = l - 2 ; i >= 0 ; i -- ) { let k = 0 ; while ( k < vec [ i + 1 ] . length - 1 ) { vec [ i ] . push ( vec [ i + 1 ] [ k ] + vec [ i + 1 ] [ k + 1 ] ) ; k += 2 ; } } let sum = 0 ; for ( let i = 0 ; i < l ; i ++ ) { for ( let j = 0 ; j < vec [ i ] . length ; j ++ ) { sum += vec [ i ] [ j ] ; } } return sum ; } let l = 3 ; document . write ( sumNodes ( l ) ) ;