Facebook
From Abrupt Motmot, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 49
  1. class INT:
  2.     def __init__(self):
  3.         self.a = 0
  4.  
  5. class Node:
  6.     def __init__(self, data):
  7.         self.left = None
  8.         self.right = None
  9.         self.data = data
  10.  
  11. def newNode(data):
  12.     node = Node(data)
  13.     return node
  14.  
  15.  
  16. def height(root):
  17.     if (root == None):
  18.         return 0;
  19.     lheight = height(root.left)
  20.     rheight = height(root.right)
  21.     return max(lheight, rheight) + 1
  22.  
  23.  
  24. def LevelOrder(root, level, count):
  25.     if (root == None):
  26.         return;
  27.     if (level == 1 and
  28.         root.left != None and
  29.        root.right != None):
  30.         count.a += 1
  31.     elif (level > 1):
  32.         LevelOrder(root.left,
  33.                    level - 1, count)
  34.         LevelOrder(root.right,
  35.                    level - 1, count)
  36.  
  37. def CountFullNodes(root, kids):
  38.     h = height(root)
  39.     count = INT()
  40.     LevelOrder(root, kids, count)
  41.     return count.a
  42.  
  43. if __name__ == "__main__":
  44.     root = newNode(7)
  45.     root.left = newNode(5)
  46.     root.right = newNode(6)
  47.     root.left.left = newNode(8)
  48.     root.left.right = newNode(1)
  49.     root.left.left.left = newNode(2)
  50.     root.left.left.right = newNode(11)
  51.     root.right.left = newNode(3)
  52.     root.right.right = newNode(9)
  53.     root.right.right.right = newNode(13)
  54.     root.right.right.left = newNode(10)
  55.     root.right.right.right.left = newNode(4)
  56.     root.right.right.right.right = newNode(12)
  57.     kids = 2
  58.  
  59.     print(CountFullNodes(root, kids))