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