Print path from root to all nodes in a Complete Binary Tree | Function to print path of all the nodes nth node represent as given node kth node represents as left and right node ; base condition if kth node value is greater then nth node then its means kth node is not valid so we not store it into the res simply we just return ; Storing node into res ; Print the path from root to node ; store left path of a tree So for left we will go node ( kThNode * 2 ) ; right path of a tree and for right we will go node ( kThNode * 2 + 1 ) ; Function to print path from root to all of the nodes ; res is for store the path from root to particulate node ; Print path from root to all node . third argument 1 because of we have to consider root node is 1 ; Given Node ; Print path from root to all node .
function printPath ( res , nThNode , kThNode ) { if ( kThNode > nThNode ) return ; res . push ( kThNode ) ; for ( var i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ; document . write ( " " ) ; printPath ( res , nThNode , kThNode * 2 ) ; printPath ( res , nThNode , kThNode * 2 + 1 ) ; res . pop ( ) } function printPathToCoverAllNodeUtil ( nThNode ) { var res = [ ] ; printPath ( res , nThNode , 1 ) ; } var nThNode = 7 ; printPathToCoverAllNodeUtil ( nThNode ) ;