Sorted Numbers
Explore how to solve the problem of listing numbers from 1 to a given number lexicographically by implementing a trie. Understand insertion of integers into the trie, recursive preorder traversal, and optimization techniques to efficiently generate sorted numbers within specified limits.
Problem statement
Given an integer
Example 1
Sample input
n = 13
Sample output
[1,10,11,12,13,2,3,4,5,6,7,8,9]
Explanation
Sorting the values lexicographically [1,2,3,4,5,6,7,8,9,10,11,12,13] results in [1,10,11,12,13,2,3,4,5,6,7,8,9]
Example 2
Sample input
n = 3
Sample output
[1,2,3]
Explanation
Sorting the values [1,2,3] lexicographically, results in [1,2,3]
Try it yourself
Try to solve the problem yourself before reading the solution.
Intuition
The first idea that strikes the mind is to iterate through all the integers from