Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean livelloK(Albero a, int k) {
- int sommaK = contaK(a,k,1);
- int sommaFoglie = contaFoglie(a);
- if (sommaK == sommaFoglie) {
- return true;
- }
- return false;
- }
- public static int contaK(Albero a, int k, int livelloCorrente) {
- if (livelloCorrente == k) {
- return a.val();
- }
- else {
- int conta = 0;
- if (a.sinistro() != null) {
- conta += contaK(a.sinistro(),k, livelloCorrente + 1);
- }
- if (a.destro() != null) {
- conta += contaK(a.destro(),k, livelloCorrente + 1);
- }
- return conta;
- }
- }
- public static int contaFoglie(Albero a) {
- int conta = 0;
- if (a.sinistro() == null && a.destro() == null) { // E' FOGLIA!
- return a.val();
- }
- if (a.sinistro() != null) {
- conta += contaFoglie(a.sinistro());
- }
- if (a.destro() != null) {
- conta += contaFoglie(a.destro());
- }
- return conta;
- }
Advertisement
Add Comment
Please, Sign In to add comment