Untitled

 avatar
unknown
python
2 months ago
592 B
2
Indexable
      18
     /  \
    10   35
   /  \  /  \
  3    5 7   40
 / \  / \
2  12 21 1


A=2 e B=12

def sono_sorelle(root, A, B):
    
    if root is None:
        return False
    
    if root.left != None and root.right != None:
        if (root.left.val == A and root.right.val == B) or
        (root.left.val == B and root.right.val == A):
            return True
    
    trovato_sinistra = sono_sorelle(root.left, A,B)
    if trovato_sinistra:
        return True
    
    trovato_destra = sono_sorelle(root.right, A,B)
    if trovato_destra:
        return True
    
    return False
    
Editor is loading...
Leave a Comment