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 ; Driver Code ; Given Node ; Print path from root to all node .
def printPath ( res , nThNode , kThNode ) : NEW_LINE INDENT if kThNode > nThNode : NEW_LINE INDENT return NEW_LINE DEDENT res . append ( kThNode ) NEW_LINE for i in range ( 0 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE printPath ( res [ : ] , nThNode , kThNode * 2 ) NEW_LINE printPath ( res [ : ] , nThNode , kThNode * 2 + 1 ) NEW_LINE DEDENT def printPathToCoverAllNodeUtil ( nThNode ) : NEW_LINE INDENT res = [ ] NEW_LINE printPath ( res , nThNode , 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT nThNode = 7 NEW_LINE printPathToCoverAllNodeUtil ( nThNode ) NEW_LINE DEDENT