<p>Given an integer <code>n</code>, return all the numbers in the range <code>[1, n]</code> sorted in lexicographical order.</p>

<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses <code>O(1)</code> extra space.&nbsp;</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> n = 13
<strong>Output:</strong> [1,10,11,12,13,2,3,4,5,6,7,8,9]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> n = 2
<strong>Output:</strong> [1,2]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>
</ul>

class TrieNode:
  def __init__(self):
    self.children = {}
    self.end = False


class Solution:
    def lexicalOrder(self, n: int) -> List[int]:
      trie = TrieNode()
      for i in range(1, n+1):
        cur = trie
        for c in str(i):
          if c not in cur.children:
            cur.children[c] = TrieNode()
          cur = cur.children[c]
        cur.end = True

      cur = trie
      res = []
      
      def dfs(node, s):
        if node.end:
          res.append(int("".join(s)))
        for i in range(10):
          c = str(i)
          if c in node.children:
            dfs(node.children[c], s + [c])
      dfs(cur, [])
      return res
        




        
28500