What is wrong with this method in java? I want implement recursion

I have the following method, but I want to use recursion; however, I get an error: "missing return statement".

static String buscar(NodoDeArbol raiz, String letra) {
    if(raiz == null) {
        aux="";
        for (int i = 0; i < auxiliar.length()-1; i++) {
            aux+=auxiliar.charAt(i);
        }
        return aux;
    }
    auxiliar = buscar(raiz.izquierdo, auxiliar+= "0");
    auxiliar = buscar(raiz.derecho, auxiliar+= "1");
}

What should to do to fix this?

Upvotes: 1

Views: 98

Answers (4)

Kyle
Kyle

Reputation: 14666

You're if statement has the only return. You will need to add a return statement for the case that is not included in your if statement. Just judging by your current code, I'm guessing you meant to have return auxiliar; at the end of your method.

Upvotes: 2

Marcos
Marcos

Reputation: 4643

There are a couple of errors

  • First, you don't define aux (maybe is global?)
  • Second, you need to return a value when raiz != null

Upvotes: 3

Miloš Lukačka
Miloš Lukačka

Reputation: 842

wrong is, that you return only when raiz is null, when you get parameter raiz, that is not null, method would never end - cause there is no return outside of if statement

Upvotes: 1

Kevin
Kevin

Reputation: 3509

You are only returning a string when the case is null, but not returning anything when the case is not null.

You need to handle all cases. A return method (String) MUST return some sort of value.

Upvotes: 2

Related Questions