Count full nodes in a Binary tree ( Iterative and Recursive ) | A node structure ; Iterative Method to count full nodes of binary tree ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize count ; initialize count for full nodes ; Enqueue left child ; Enqueue right child ; 2 / \ 7 5 \ \ 6 9 / \ / 1 11 4 Let us create Binary Tree as shown
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getfullCount ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT queue = [ ] NEW_LINE queue . append ( root ) NEW_LINE count = 0 NEW_LINE while ( len ( queue ) > 0 ) : NEW_LINE INDENT node = queue . pop ( 0 ) NEW_LINE if node . left is not None and node . right is not None : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if node . left is not None : NEW_LINE INDENT queue . append ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT queue . append ( node . right ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 7 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . left . right . left = Node ( 1 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 4 ) NEW_LINE print " % d " % ( getfullCount ( root ) ) NEW_LINE