Count even paths in Binary Tree | A Tree node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Driver Code ; Tree ; Function call
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . key = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def evenPaths ( node , count ) : NEW_LINE INDENT if ( node == None or ( node . key % 2 != 0 ) ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( not node . left and not node . right ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT count = evenPaths ( node . left , count ) NEW_LINE return evenPaths ( node . right , count ) NEW_LINE DEDENT def countEvenPaths ( node ) : NEW_LINE INDENT return evenPaths ( node , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 12 ) NEW_LINE root . left = Node ( 13 ) NEW_LINE root . right = Node ( 12 ) NEW_LINE root . right . left = Node ( 14 ) NEW_LINE root . right . right = Node ( 16 ) NEW_LINE root . right . left . left = Node ( 21 ) NEW_LINE root . right . left . right = Node ( 22 ) NEW_LINE root . right . right . left = Node ( 22 ) NEW_LINE root . right . right . right = Node ( 24 ) NEW_LINE root . right . right . right . left = Node ( 8 ) NEW_LINE print ( countEvenPaths ( root ) ) NEW_LINE DEDENT