Generate all numbers up to N in Lexicographical Order | Java program for the above approach ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static void lexNumbers ( int n ) { List < Integer > sol = new ArrayList < > ( ) ; dfs ( 1 , n , sol ) ; System . out . println ( sol ) ; } public static void dfs ( int temp , int n , List < Integer > sol ) { if ( temp > n ) return ; sol . add ( temp ) ; dfs ( temp * 10 , n , sol ) ; if ( temp % 10 != 9 ) dfs ( temp + 1 , n , sol ) ; } public static void main ( String [ ] args ) { int n = 15 ; lexNumbers ( n ) ; } }