Salve a tutti,
è un esercizio banale lo so ma non riesco a venirne a capo.

questo è il testo:
Scrivere il metodo Java static int sommaIntervallo(TreeNode radice, int min, int max) che re-
stituisce la somma dei soli nodi il cui valore è compreso tra min e max, sfruttando la struttura del BST per
evitare di visitare inutilmente porzioni di rami dell'albero. Calcolare il costo computazionale, motivandolo
opportunamente.

Questo è quello che ho scritto io, mi pare corretto logicamente, però testandolo non funziona come dovrebbe.

codice:
    public int stampaIntervallo(BSTNode root, int min, int max) {
        int somma = 0;
        if (root == null) return 0;

        if ((min < root.key) && (root.key < max)) {
            somma += root.key;
            if (root.left.key > min)
                somma += stampaIntervallo(root.left, min, max);
            if (root.right.key < max)
                somma += stampaIntervallo(root.right, min, max);
        }
       
        if (root.key > max)
            somma += stampaIntervallo(root.left, min, max);
        if (root.key < min)
            somma += stampaIntervallo(root.right, min, max);
       
        return somma;

    }