davegimo

cerca nodo

Aug 20th, 2023
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. def cercavalore(a, k):
  2.     if a is None:
  3.         return False
  4.  
  5.     if a.val == k:
  6.         return True
  7.  
  8.     if a.sx is None a.dx is None:
  9.         return False
  10.  
  11.     cercasx = False
  12.     cercadx = False
  13.  
  14.     if a.sx is not None:
  15.         cercasx = cercavalore(a.sx, k)
  16.  
  17.     if a.dx is not None:
  18.         cercadx = cercavalore(a.dx, k)
  19.  
  20.  
  21.     return cercasx or cercadx
  22.  
  23.  
  24. def cercavalore2(a,k):
  25.     if a is None:
  26.         return False
  27.  
  28.     if a.val == k:
  29.         return True
  30.  
  31.     return cercavalore2(a.sx,k) or cercavalore2(a.dx,k)
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment