Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;

int main() {
  queue<long long> Q;
  int k;
  long long num = 0;
  long long ans;
  cin >> k;

  if (k <= 9) {
    ans = k;
  } else {
    for (int i = 1; i <= 9; i++) {
      Q.push(i);
      num++;
    }
    while (num < k) {
      if (Q.front() % 10 == 0) {
        ans = Q.front() * 10;
        if (num < k - 1) {
          Q.push(ans);
          Q.push(ans + 1);
          ans = ans + 1;
          num += 2;
          Q.pop();
        } else {
          break;
        }
      } else if (Q.front() % 10 == 9) {
        ans = Q.front() * 10 + 8;
        if (num < k - 1) {
          Q.push(ans);
          Q.push(ans + 1);
          num += 2;
          ans = ans + 1;
          Q.pop();
        } else {
          break;
        }
      } else {
        ans = Q.front() * 10 + Q.front() % 10 - 1;
        long long p = ans;
        for (int i = 0; i < 3 && num < k; i++) {
          ans = p + i;
          Q.push(ans);
          num++;
        }
        Q.pop();
      }
    }
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

typedef struct tree {
  long long parent;
  long long left;
  long long right;
} Tree;

Tree t[100010];

long long k;
long long x = 0;

long long makeson(long long n) {
  long long ans;
  if (n == 0) {
    t[0].left = 1;
    for (int i = 1; i <= 8; i++) {
      t[i].left = i + 1;
      x++;
      if (x == k) {
        ans = i + 1;
        goto EXIT;
      }
    }
    for (int i = 1; i <= 9; i++) {
      makeson(i);
    }
  } else {
    if (n % 10 == 0) {
      t[n].left = n * 10;
      x++;
      if (x == k) {
        ans = n * 10;
        goto EXIT;
      }
      t[t[n].left].right = n * 10 + 1;
      x++;
      if (x == k) {
        x = n * 10 + 1;
        goto EXIT;
      }
      makeson(t[n].left);
      makeson(t[t[n].left].right);
    } else if (n % 10 == 9) {
      t[n].left = n * 10 + 8;
      x++;
      if (x == k) {
        ans = n * 10 + 8;
        goto EXIT;
      }
      t[t[n].left].right = n * 10 + 9;
      x++;
      if (x == k) {
        ans = n * 10 + 9;
        goto EXIT;
      }
      makeson(t[n].left);
      makeson(t[t[n].left].right);
    } else {
      t[n].left = n * 10 + n - 1;
      x++;
      if (x == k) {
        ans = n * 10 + n - 1;
        goto EXIT;
      }
      t[t[n].left].right = n * 10 + n;
      x++;
      if (x == k) {
        ans = n * 10 + n;
        goto EXIT;
      }
      t[t[t[n].left].left].left = n * 10 + n + 1;
      x++;
      if (x == k) {
        ans = n * 10 + n + 1;
        goto EXIT;
      }
      makeson(t[n].left);
      makeson(t[t[n].left].right);
      makeson(t[t[t[n].left].left].left);
    }
  }
EXIT:
  return ans;
}

int main() {
  cin >> k;

  cout << makeson(0) << endl;
}