fork download
  1. class Node:
  2. """
  3. prototype for node
  4. """
  5. def __init__(self, data):
  6. self.data = data
  7. self.left = self.right = None
  8.  
  9. # Function to get the count of leaf
  10. # Nodes in a binary tree
  11. def getLeafCount(node):
  12. """
  13. input: A node
  14. return: count of leaf nodes
  15. """
  16.  
  17. # If tree is empty
  18. if (not node):
  19. return 0
  20.  
  21. # Initialize empty list.
  22. l = []
  23.  
  24. # Initialize count of leaves
  25. count = 0
  26.  
  27. # apppend root node in list
  28. l.append(node)
  29.  
  30. # Do level order traversal
  31. # starting from root
  32. while (len(l) > 0):
  33. """
  34. loop is continue as long as list l contain the elements
  35. """
  36. # create temporary variable that contain the current node
  37. temp = l[0]
  38.  
  39. # remove current node from list after creating reference temp
  40. l.remove(l[0])
  41.  
  42. if (temp.left != None): # if left child is not None append it to list
  43. l.append(temp.left)
  44. if (temp.right != None): # if right child is not None append it to list
  45. l.append(temp.right)
  46. # if both child is None, means it is leaf node increse the count
  47. if (temp.left == None and temp.right == None):
  48. count += 1
  49. return count
  50.  
  51. def main():
  52. root = Node(1)
  53. root.left = Node(2)
  54. root.right = Node(3)
  55. root.left.left = Node(4)
  56. root.left.right = Node(5)
  57. print("count of leaf node is",getLeafCount(root))
  58.  
  59. if __name__ == '__main__':
  60. main()
  61.  
  62. <!--count of leaf node is 3-->
  63.  
Success #stdin #stdout 0.02s 25992KB
stdin
Standard input is empty
stdout
class Node: 
    """
        prototype for node
    """
    def __init__(self, data): 
        self.data = data 
        self.left = self.right = None
		
# Function to get the count of leaf 
# Nodes in a binary tree 
def getLeafCount(node): 
    """
    input: A node
    return: count of leaf nodes
    """
    
    # If tree is empty 
    if (not node): 
        return 0
    
    # Initialize empty list. 
    l = []
    
    # Initialize count of leaves 
    count = 0 
    
    # apppend root node in list
    l.append(node) 
    
    # Do level order traversal 
    # starting from root 
    while (len(l) > 0): 
        """
            loop is continue as long as list l contain the elements
        """
        # create temporary variable that contain the current node
        temp = l[0]
        
        # remove current node from list after creating reference temp
        l.remove(l[0])
    
        if (temp.left != None): # if left child is not None append it to list
            l.append(temp.left) 
        if (temp.right != None): # if right child is not None append it to list
            l.append(temp.right)           
        # if both child is None, means it is leaf node increse the count
        if (temp.left == None and temp.right == None): 
            count += 1
    return count 

def main():
    root = Node(1) 
    root.left = Node(2) 
    root.right = Node(3) 
    root.left.left = Node(4) 
    root.left.right = Node(5) 
    print("count of leaf node is",getLeafCount(root)) 
    
if __name__ == '__main__': 	
    main()
    
    <!--count of leaf node is 3-->