Print all root to leaf paths of an N | C ++ program for the above approach ; Structure of an N ary tree node ; Parameterized Constructor ; 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 ; Driver Code ; Given N - Ary tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; vector < Node * > child ; Node ( int x ) : data ( x ) { } } ; void printPath ( vector < int > vec ) { for ( int ele : vec ) { cout << ele << " ▁ " ; } cout << endl ; } void printAllRootToLeafPaths ( Node * root , vector < int > vec ) { if ( ! root ) return ; vec . push_back ( root -> data ) ; if ( root -> child . empty ( ) ) { printPath ( vec ) ; vec . pop_back ( ) ; return ; } for ( int i = 0 ; i < root -> child . size ( ) ; i ++ ) printAllRootToLeafPaths ( root -> child [ i ] , vec ) ; } void printAllRootToLeafPaths ( Node * root ) { if ( ! root ) return ; vector < int > vec ; printAllRootToLeafPaths ( root , vec ) ; } int main ( ) { Node * root = new Node ( 1 ) ; ( root -> child ) . push_back ( new Node ( 2 ) ) ; ( root -> child ) . push_back ( new Node ( 3 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( new Node ( 4 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 5 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 6 ) ) ; ( root -> child [ 1 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 7 ) ) ; ( root -> child [ 1 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; return 0 ; }