Advertisement
davegimo

sommaparialberi

Jan 22nd, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. public class Albero {
  2.  
  3.     public static int sommaNodiPari(Albero a) {
  4.  
  5.     return sommaAusiliarePari(a.left(), 2) + sommaAusiliarePari(a.right(), 2);  //perche la root "a" sta a livello 1
  6.  
  7.     }
  8.  
  9.     public static int sommaAusiliarePari(Albero a, int livello) {
  10.        
  11.         int conta = 0;
  12.  
  13.         if (livello % 2 == 0)  {  // aumento SOLO SE IL LIVELLO E´PARI
  14.             conta += a.val();
  15.         }
  16.        
  17.         if (a.left() != null) {
  18.             conta += sommaAusiliarePari(a.left(), livello + 1);
  19.         }
  20.  
  21.         if (a.right() != null) {
  22.             conta += sommaAusiliarePari(a.right(), livello + 1);
  23.         }
  24.  
  25.         return conta;
  26.        
  27.  
  28.    
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement