public class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> res = new LinkedList<>();
for (int i = 1; i < 10; ++i) {
dfs(res, i, n);
}
return res;
}
private void dfs(List<Integer> res, int path, int n) {
if (path <= n) {
res.add(path);
} else {
return;
}
for (int i = 0; i < 10; ++i) {
dfs(res, path * 10 + i, n);
}
}
}