Print path from root to a given node in a binary tree | C # implementation to print the path from root to a given node in a binary tree ; A node of binary tree ; Returns true if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is NULL there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr '  ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ▁ ' arr 'and then return false  ; function to print the path from root to the given node if the node lies in the binary tree ; List to store the path ; if required node ' x ' is present then print the path ; ' x ' is not present in the binary tree ; Driver code ; binary tree formation
using System ; using System . Collections ; using System . Collections . Generic ; class PrintPath { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public static Boolean hasPath ( Node root , List < int > arr , int x ) { if ( root == null ) return false ; arr . Add ( root . data ) ; if ( root . data == x ) return true ; if ( hasPath ( root . left , arr , x ) || hasPath ( root . right , arr , x ) ) return true ; arr . RemoveAt ( arr . Count - 1 ) ; return false ; } public static void printPath ( Node root , int x ) { List < int > arr = new List < int > ( ) ; if ( hasPath ( root , arr , x ) ) { for ( int i = 0 ; i < arr . Count - 1 ; i ++ ) Console . Write ( arr [ i ] + " - > " ) ; Console . Write ( arr [ arr . Count - 1 ] ) ; } else Console . Write ( " No ▁ Path " ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; int x = 5 ; printPath ( root , x ) ; } }