davegimo

esercizio cammini

Dec 28th, 2022
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #contare elementi == k
  2. def conta(A,k):
  3.     if A is None:
  4.         return 0
  5.  
  6.     contavalore = 0
  7.     if A.val == k:
  8.         contavalore += 1
  9.  
  10.     return contavalore + conta(a.sx,k) + conta(a.dx,k)
  11.  
  12.  
  13. def verifica_almeno_2(A,k):
  14.     return conta(A,k) >= 2
  15.  
  16.  
  17. #########
  18.  
  19. def es1(A,B):
  20.     if A is None:
  21.         return 0
  22.  
  23.     if A.sx is None and A.dx is None:
  24.         return verifica_almeno_2(B,a.val)
  25.    
  26.     return somma_cammini_foglie(A.sx,B,A.val) or somma_cammini_foglie(A.dx,B,A.val)
  27.  
  28. def somma_cammini_foglie(A,B, somma_corrente):
  29.     if A is None:
  30.         return False
  31.  
  32.     if A.sx is None and A.dx is None:
  33.         return verifica_almeno_2(B,somma_corrente + a.val)
  34.  
  35.  
  36.     return somma_cammini_foglie(A.sx,B, somma_corrente + a.val) or somma_cammini_foglie(A.dx,B, somma_corrente + a.val)
  37.  
  38.    
Advertisement
Add Comment
Please, Sign In to add comment