Number of subtrees having odd count of even numbers | Helper class that allocates a new Node with the given data and None left and right pointers . ; Returns count of subtrees having odd count of even numbers ; base condition ; count even nodes in left subtree ; Add even nodes in right subtree ; Check if root data is an even number ; if total count of even numbers for the subtree is odd ; Total count of even nodes of the subtree ; A wrapper over countRec ( ) ; Driver Code ; binary tree formation 2 ; / \ ; 1 3 ; / \ / \ ; 4 10 8 5 ; / ; 6 ;
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 countRec ( root , pcount ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT c = countRec ( root . left , pcount ) NEW_LINE c += countRec ( root . right , pcount ) NEW_LINE if ( root . data % 2 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if c % 2 != 0 : NEW_LINE INDENT pcount [ 0 ] += 1 NEW_LINE DEDENT return c NEW_LINE DEDENT def countSubtrees ( root ) : NEW_LINE INDENT count = [ 0 ] NEW_LINE pcount = count NEW_LINE countRec ( root , pcount ) NEW_LINE return count [ 0 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 2 ) NEW_LINE root . left = newNode ( 1 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 10 ) NEW_LINE root . right . left = newNode ( 8 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . left . right . left = newNode ( 6 ) NEW_LINE print ( " Count ▁ = ▁ " , countSubtrees ( root ) ) NEW_LINE DEDENT