Count nodes having highest value in the path from root to itself in a Binary Tree | ''Python 3 program for the above approach ; ''Stores the ct of nodes which are maximum in the path from root to the current node ; ''Binary Tree Node ; ''Function that performs Inorder Traversal on the Binary Tree ; '' If root does not exist ; '' Check if the node  satisfies the condition ; '' Update the maximum value  and recursively traverse  left and right subtree ; ''Function that counts the good nodes in the given Binary Tree ; '' Perform inorder  Traversal ; '' Return the final count ; ''Driver code ; ''   A Binary Tree  3  / /  2 5  / /  4 6     ; '' Function call ; '' Print the count of good nodes
import sys NEW_LINE ct = 0 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def find ( root , mx ) : NEW_LINE INDENT global ct NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . val >= mx ) : NEW_LINE INDENT ct += 1 NEW_LINE DEDENT find ( root . left , max ( mx , root . val ) ) NEW_LINE find ( root . right , max ( mx , root . val ) ) NEW_LINE DEDENT def NodesMaxInPath ( root ) : NEW_LINE INDENT global ct NEW_LINE find ( root , - sys . maxsize - 1 ) NEW_LINE return ct NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 3 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE answer = NodesMaxInPath ( root ) NEW_LINE print ( answer ) NEW_LINE DEDENT