Binary Search Tree In-Order Traversal per un nuovo array

voti
1

Ho fatto un attraversamento BST in ordine, mentre la stampa fuori per la console come un esercizio, ma il compito è stato quello di aggiungerlo in un nuovo elenco ...

Ho provato a fare un modo simile con la creazione della lista al di fuori del metodo e incrementando il valore 'x', mentre aggiungendo alla array [i] lista, ma continuo a ricevere un NullPointerException

Qualcuno può aiutarmi a capire il motivo?

int[] bstArray;
int x = 0;

public int[] returnInOrderTraversal(BSTNode node) {
    if(node == null) return bstArray;

    if(node.getLeftChild() != null) {
        returnInOrderTraversal(node.getLeftChild());
    }

    bstArray[x] = node.getValue();
    x++;

    if(node.getRightChild() != null) {
        returnInOrderTraversal(node.getRightChild());
    }

    return bstArray;
}

Grazie

È pubblicato 16/03/2015 alle 14:43
fonte dall'utente
In altre lingue...                            


1 risposte

voti
5
int[] bstArray;  <-------- This line does not create the Array

È effettivamente bisogno di inizializzare l'array

int[] bstArray=new bstArray[someLength]; <------- like this
then use 
bstArray[x] = node.getValue();
Risposto il 16/03/2015 a 14:45
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more