fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Nodo {
  5. int dato;
  6. Nodo *izq;
  7. Nodo *der;
  8. };
  9.  
  10. Nodo *nuevoNodo(int valor) {
  11. Nodo *nodo = new Nodo;
  12. nodo->dato = valor;
  13. nodo->izq = nullptr;
  14. nodo->der = nullptr;
  15. return nodo;
  16. }
  17.  
  18. Nodo *insertar(Nodo *r, int valor) {
  19. if (r == nullptr) return nuevoNodo(valor);
  20. if (valor < r->dato) r->izq = insertar(r->izq, valor);
  21. else r->der = insertar(r->der, valor);
  22. return r;
  23. }
  24.  
  25. string buscar(Nodo *raiz, int x) {
  26. string s = "";
  27. Nodo *r = raiz;
  28.  
  29. while (r != nullptr) {
  30. if (x == r->dato) {
  31. if (s.empty()) return "RAIZ";
  32. else return s;
  33. }
  34. if (x < r->dato) {
  35. s = s + 'L';
  36. r = r->izq;
  37. } else {
  38. s = s + 'R';
  39. r = r->der;
  40. }
  41. }
  42. return "NO EXISTE";
  43. }
  44.  
  45. int main() {
  46. int q;
  47. if (!(cin >> q)) return 0;
  48.  
  49. Nodo *raiz = nullptr;
  50. char operacion;
  51. int x;
  52.  
  53. for (int i = 0; i < q; i++) {
  54. cin >> operacion >> x;
  55.  
  56. if (operacion == 'I') {
  57. raiz = insertar(raiz, x);
  58. } else if (operacion == 'R') {
  59. string resultado = buscar(raiz, x);
  60. cout << resultado << "\n";
  61. }
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 5320KB
stdin
10

I 50

I 30

I 70

I 20

I 40

R 40

R 20

R 70

I 35

R 35
stdout
LR
LL
R
LRL