Print the path common to the two paths from the root to the two given nodes | Java implementation to print the path common to the two paths from the root to the two given nodes ; Initialize n1 and n2 as not visited ; Class containing left and right child of current node and key value ; This function returns pointer to LCA of two given values n1 and n2 . This function assumes that n1 and n2 are present in Binary Tree ; Base case ; Store result in temp , in case of key match so that we can search for other key also . ; If either n1 or n2 matches with root 's key, report the presence  by setting v1 or v2 as true and return root (Note that if a key is ancestor of other, then the ancestor key becomes LCA) ; Look for keys in left and right subtrees ; If both of the above calls return Non - NULL , then one key is present in once subtree and other is present in other , So this node is the LCA ; Otherwise check if left subtree or right subtree is LCA ; Returns true if key k is present in tree rooted with root ; Base Case ; If key k is present at root , or in left subtree or right subtree , return true ; Else return false ; This function returns LCA of n1 and n2 only if both n1 and n2 are present in tree , otherwise returns null ; Find lca of n1 and n2 ; Return LCA only if both n1 and n2 are present in tree ; Else return null ; function 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 there 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 common to the two paths from the root to the two given nodes if the nodes lie in the binary tree ; ArrayList to store the common path ; LCA of node n1 and n2 ; if LCA of both n1 and n2 exists ; then print the path from root to LCA node ; LCA is not present in the binary tree either n1 or n2 or both are not present ; Driver code
import java . util . ArrayList ; public class PrintCommonPath { static boolean v1 = false , v2 = false ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } static Node findLCAUtil ( Node node , int n1 , int n2 ) { if ( node == null ) return null ; Node temp = null ; if ( node . data == n1 ) { v1 = true ; temp = node ; } if ( node . data == n2 ) { v2 = true ; temp = node ; } Node left_lca = findLCAUtil ( node . left , n1 , n2 ) ; Node right_lca = findLCAUtil ( node . right , n1 , n2 ) ; if ( temp != null ) return temp ; if ( left_lca != null && right_lca != null ) return node ; return ( left_lca != null ) ? left_lca : right_lca ; } static boolean find ( Node root , int k ) { if ( root == null ) return false ; if ( root . data == k || find ( root . left , k ) || find ( root . right , k ) ) return true ; return false ; } static Node findLCA ( Node root , int n1 , int n2 ) { Node lca = findLCAUtil ( root , n1 , n2 ) ; if ( v1 && v2 || v1 && find ( lca , n2 ) || v2 && find ( lca , n1 ) ) return lca ; return null ; } static boolean hasPath ( Node root , ArrayList < Integer > 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 . remove ( arr . size ( ) - 1 ) ; return false ; } static void printCommonPath ( Node root , int n1 , int n2 ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; Node lca = findLCA ( root , n1 , n2 ) ; if ( lca != null ) { if ( hasPath ( root , arr , lca . data ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) System . out . print ( arr . get ( i ) + " - > " ) ; System . out . print ( arr . get ( arr . size ( ) - 1 ) ) ; } } else System . out . print ( " No ▁ Common ▁ 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 ) ; root . left . right . left = new Node ( 8 ) ; root . right . left . right = new Node ( 9 ) ; int n1 = 4 , n2 = 8 ; printCommonPath ( root , n1 , n2 ) ; } }