1

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?

1
  • 1
    If the return type of your method is not void- it has to return something of the declared type. I also suggest that you post the relevant part of your code into your question. Commented Apr 10, 2013 at 21:42

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.