text: 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 .
code: < ? php function printPath ( $ res , $ nThNode , $ kThNode ) { if ( $ kThNode > $ nThNode ) return ; array_push ( $ res , $ kThNode ) ; for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo $ res [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; printPath ( $ res , $ nThNode , $ kThNode * 2 ) ; printPath ( $ res , $ nThNode , $ kThNode * 2 + 1 ) ; } function printPathToCoverAllNodeUtil ( $ nThNode ) { $ res = array ( ) ; printPath ( $ res , $ nThNode , 1 ) ; } $ nThNode = 7 ; printPathToCoverAllNodeUtil ( $ nThNode ) ; ? >