Count Non | class that allocates a new node with the given data and None left and right pointers . ; Computes the number of non - leaf nodes in a tree . ; Base cases . ; If root is Not None and its one of its child is also not None ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def countNonleaf ( root ) : NEW_LINE INDENT if ( root == None or ( root . left == None and root . right == None ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countNonleaf ( root . left ) + countNonleaf ( root . right ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE print ( countNonleaf ( root ) ) NEW_LINE DEDENT