Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BinaryTree:
- def __init__(self, val):
- self.left = None
- self.right = None
- self.val = val
- def getRight(self):
- return self.right
- def getLeft(self):
- return self.left
- def getVal(self):
- return self.val
- def sommaNodi(a):
- if a.getRight() == None and a.getLeft() == None: #è fogliaa!!!!
- return a.getVal()
- somma = a.getVal()
- if a.getLeft() != None:
- somma += sommaNodi(a.getLeft())
- if a.getRight() != None:
- somma += sommaNodi(a.getRight())
- return somma
- def contaNodi(a):
- if a.getRight() == None and a.getLeft() == None: #è fogliaa!!!!
- return 1
- conta = 1
- if a.getLeft() != None:
- conta += contaNodi(a.getLeft())
- if a.getRight() != None:
- conta += contaNodi(a.getRight())
- return conta
- def contaNodiPari(a):
- if a.getVal() % 2 == 0:
- conta = 1
- else:
- conta = 0
- if a.getRight() == None and a.getLeft() == None: #è fogliaa!!!!
- return conta
- if a.getLeft() != None:
- conta += contaNodiPari(a.getLeft())
- if a.getRight() != None:
- conta += contaNodiPari(a.getRight())
- return conta
- a = BinaryTree(4)
- b = BinaryTree(1)
- c = BinaryTree(3)
- d = BinaryTree(5)
- dd = BinaryTree(55)
- a.left = b
- a.right = c
- c.right = d
- d.left = dd
- print(contaNodiPari(a))
Advertisement
Add Comment
Please, Sign In to add comment