Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Albero {
- public static boolean foglieContenute(Albero a, Albero b, Albero c) {
- int foglieA = sommaFoglie(a);
- int nodiB = sommaNodi(b);
- int nodiC = sommaNodi(c);
- return (foglieA == nodiB + nodiC);
- }
- public static int sommaNodi(Albero k) {
- int conta = k.val();
- if (k.sinistro() != null) {
- conta += sommaNodi(k.sinistro());
- }
- if (k.destro() != null) {
- conta += sommaNodi(k.destro());
- }
- return conta;
- }
- public static int sommaFoglie(Albero k) {
- if (k.sinistro() == null && k.destro() == null) { // E' FOGLIA!!
- return k.val();
- }
- int conta = 0;
- if (k.sinistro() != null) {
- conta += sommaFoglie(k.sinistro());
- }
- if (k.destro() != null) {
- conta += sommaFoglie(k.destro());
- }
- return conta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement