Print all root to leaf paths of an N |  ; 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
using System ; using System . Collections . Generic ; public class Node { public int data ; public List < Node > child ; public Node ( int x ) { this . data = x ; this . child = new List < Node > ( ) ; } } public class GFG { static void printPath ( List < int > vec ) { foreach ( int ele in vec ) { Console . Write ( ele + " ▁ " ) ; } Console . WriteLine ( ) ; } static void printAllRootToLeafPaths ( Node root , List < int > vec ) { if ( root == null ) return ; vec . Add ( root . data ) ; if ( root . child . Count == 0 ) { printPath ( vec ) ; vec . RemoveAt ( vec . Count - 1 ) ; return ; } for ( int i = 0 ; i < root . child . Count ; i ++ ) { printAllRootToLeafPaths ( root . child [ i ] , vec ) ; } vec . RemoveAt ( vec . Count - 1 ) ; } static void printAllRootToLeafPaths ( Node root ) { if ( root == null ) return ; List < int > vec = new List < int > ( ) ; printAllRootToLeafPaths ( root , vec ) ; } static public void Main ( ) { Node root = new Node ( 1 ) ; ( root . child ) . Add ( new Node ( 2 ) ) ; ( root . child ) . Add ( new Node ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 4 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 5 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 7 ) ) ; ( root . child [ 1 ] . child [ 1 ] . child ) . Add ( new Node ( 8 ) ) ; printAllRootToLeafPaths ( root ) ; } }