1 votes

How to make the code walk through the entire binary tree?

The code I am doing about how to find a NODE in a BINARY TREE does not work, it only works for the nodes that are directly to the left of the ROOT of the tree.

Below I write the code

 public Nodo encontrarNodo(Nodo raiz, String dato){

        if(raiz!=null){
                if(raiz.getIzquierda()!=null){

                    if(dato.equals(raiz.getDato())){

                        return raiz;
                    }else{
                        return encontrarNodo(raiz.getIzquierda(), dato);
                    }
                }

                if(raiz.getDerecha()!=null){
                    if(dato.equals(raiz.getDato())){
                            return raiz;
                    } else{
                            return encontrarNodo(raiz.getDerecha(), dato);
                    }   
                }
        }

        return null;


    }

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X