Print all root to leaf paths of an N | Structure of an N ary tree node ; Function to print the root to leaf path of the given N - ary Tree ; Print elements in the vector ; Utility function to print all root to leaf paths of an Nary Tree ; If root is null ; Insert current node 's  data into the vector ; If current node is a leaf node ; Print the path ; Pop the leaf node and return ; Recur for all children of the current node ; Recursive Function Call ; Function to print root to leaf path ; If root is null , return ; Stores the root to leaf path ; Utility function call ; Given N - Ary tree ; Function Call
class Node { constructor ( x ) { this . data = x ; this . child = [ ] ; } } function printPath ( vec ) { for ( let ele = 0 ; ele < vec . length ; ele ++ ) { document . write ( vec [ ele ] + " " ) ; } document . write ( " " ) ; } function printAllRootToLeafPaths ( root , vec ) { if ( root == null ) return ; vec . push ( root . data ) ; if ( root . child . length == 0 ) { printPath ( vec ) ; vec . pop ( ) ; return ; } for ( let i = 0 ; i < root . child . length ; i ++ ) printAllRootToLeafPaths ( root . child [ i ] , vec ) ; vec . pop ( ) ; } function printAllRootToLeaf_Paths ( root ) { if ( root == null ) return ; let vec = [ ] ; printAllRootToLeafPaths ( root , vec ) ; } let root = new Node ( 1 ) ; ( root . child ) . push ( new Node ( 2 ) ) ; ( root . child ) . push ( new Node ( 3 ) ) ; ( root . child [ 0 ] . child ) . push ( new Node ( 4 ) ) ; ( root . child [ 1 ] . child ) . push ( new Node ( 5 ) ) ; ( root . child [ 1 ] . child ) . push ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . push ( new Node ( 7 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . push ( new Node ( 8 ) ) ; printAllRootToLeaf_Paths ( root ) ;