Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Albero:
- def __init__(self, v): #costruttore
- self.val = v
- self.sx = None
- self.dx = None
- def somma(a):
- if a is None:
- return 0
- if a.sx is None and a.dx is None:
- return a.val
- somma_parziale = a.val
- if a.sx is not None:
- somma_parziale += somma(a.sx)
- if a.dx is not None:
- somma_parziale += somma(a.dx)
- return somma_parziale
- ###main
- a = Albero(1)
- b = Albero(10)
- c = Albero(5)
- d = Albero(7)
- e = Albero(11)
- f = Albero(16)
- g = Albero(20)
- a.sx = b
- a.dx = c
- c.dx = d
- b.sx = e
- d.sx = f
- d.dx = g
- s = somma(a)
- print(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement