Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Albero:
- def __init__(self, val):
- self.val = val
- self.sx = None
- self.dx = None
- a = Albero(8)
- b = Albero(3)
- c = Albero(10)
- d = Albero(1)
- e = Albero(6)
- f = Albero(4)
- g = Albero(7)
- h = Albero(14)
- i = Albero(13)
- a.sx = b
- a.dx = c
- b.sx = d
- b.dx = e
- e.sx = f
- e.dx = g
- c.dx = h
- h.sx = i
- def sommaNodi(a):
- if a is None:
- return 0
- if a.sx is None and a.dx is None:
- return a.val
- somma = a.val
- if a.sx is not None:
- somma += sommaNodi(a.sx)
- if a.dx is not None:
- somma += sommaNodi(a.dx)
- return somma
Advertisement
Add Comment
Please, Sign In to add comment