Binární vyhledávací strom In-Order Traversal do nového pole

hlasů
1

Udělal jsem traversal BST in-pořadí, zatímco tisk na konzoli jako cvičení, ale úkolem bylo přidat do nového seznamu ...

Zkoušel jsem to dělal podobný způsob vytvořením seznamu mimo metody a zvyšování hodnoty ‚X‘, zatímco přidávání do pole [i] seznamu, ale neustále se mi NullPointerException

Může mi někdo pomoci zjistit, proč?

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;
}

dík

Položena 16/03/2015 v 14:43
zdroj uživatelem
V jiných jazycích...                            


1 odpovědí

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

Jste skutečně potřeba inicializovat pole

int[] bstArray=new bstArray[someLength]; <------- like this
then use 
bstArray[x] = node.getValue();
Odpovězeno 16/03/2015 v 14:45
zdroj uživatelem

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