Ancora alberi

This commit is contained in:
2025-11-20 09:41:46 +01:00
parent c6a896f0e2
commit 241fe7271f

View File

@@ -1,3 +1,5 @@
import static java.lang.Math.max;
class Nodo{ class Nodo{
int dato; int dato;
Nodo left, right; Nodo left, right;
@@ -62,6 +64,11 @@ class Nodo{
else if(livello==0) return albero.dato + " "; else if(livello==0) return albero.dato + " ";
else return valoriNelLivello(albero.left, livello-1) + valoriNelLivello(albero.right, livello-1); else return valoriNelLivello(albero.left, livello-1) + valoriNelLivello(albero.right, livello-1);
} }
static int altezza(Nodo albero){
if(albero==null) return 0;
else if (albero.left == null && albero.right == null) return 1;
return 1 + max(altezza(albero.left), altezza(albero.right));
}
} }