DSA Trie

Lexicographical Numbers

List 1..n in lexicographic (dictionary) order without sorting.

Medium
Time O(n) Space O(1)
LeetCode
3 min read
trie dfs math

Problem#

Given an integer n, return all numbers from 1 to n in lexicographic order. The solution must run in O(n) time and use O(1) extra space.

Examples#

  • n = 13[1,10,11,12,13,2,3,4,5,6,7,8,9].
  • n = 2[1,2].

Constraints#

  • 1 <= n <= 5 * 10^4.

Hints#

Hint
Think of an implicit 10-ary tree rooted at 0 with children cur*10 + d. Pre-order it.

Approach#

Iterative pre-order over a virtual digit trie. From cur, try multiplying by 10 (descend). If cur * 10 > n, increment and back up past trailing 9s as needed.

Solution#

Lexicographical Numbers
class Solution {
public:
vector<int> lexicalOrder(int n) {
vector<int> ans;
ans.reserve(n);
int cur = 1;
for (int i = 0; i < n; ++i) {
ans.push_back(cur);
if ((long long)cur * 10 <= n) {
cur *= 10;
} else {
while (cur % 10 == 9 || cur + 1 > n) cur /= 10;
++cur;
}
}
return ans;
}
};

Editorial#

Numbers in lexicographic order are exactly the pre-order traversal of the digit trie (root 0, children 1..9 at root, 0..9 at deeper nodes). At each step, the “next” lexicographic number is cur * 10 if it fits, otherwise cur + 1 after rolling up trailing 9s. The long long cast guards overflow when cur approaches INT_MAX / 10.

Complexity#

  • Time: O(n).
  • Space: O(1) extra (excluding output).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.