Node having maximum number of nodes less than its value in its subtree | Stores the nodes to be deleted ; Structure of a Tree node ; Function to compare the current node key with keys received from it left & right tree by Post Order traversal ; Base Case ; Find nodes lesser than the current root in the left subtree ; Find nodes lesser than the current root in the right subtree ; Stores all the nodes less than the current node 's ; Add the nodes which are less than current node in left [ ] ; Add the nodes which are less than current node in right [ ] ; Create a combined vector for pass to it 's parent ; Stores key that has maximum nodes ; Return the vector of nodes ; Driver Code ; Given Tree ; Function Call ; Print the node value
max_v = 0 NEW_LINE rootIndex = 0 NEW_LINE mp = { } NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findNodes ( root ) : NEW_LINE INDENT global max_v NEW_LINE global rootIndex NEW_LINE global mp NEW_LINE if ( root == None ) : NEW_LINE INDENT return [ ] NEW_LINE DEDENT left = findNodes ( root . left ) NEW_LINE right = findNodes ( root . right ) NEW_LINE combined = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( len ( left ) ) : NEW_LINE INDENT if ( left [ i ] < root . key ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT combined . append ( left [ i ] ) NEW_LINE DEDENT for i in range ( len ( right ) ) : NEW_LINE INDENT if ( right [ i ] < root . key ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT combined . append ( right [ i ] ) NEW_LINE DEDENT combined . append ( root . key ) NEW_LINE if ( count > max_v ) : NEW_LINE INDENT rootIndex = root . key NEW_LINE max_v = count NEW_LINE DEDENT return combined NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 3 ) NEW_LINE root . left = newNode ( 4 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . right . left = newNode ( 4 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 10 ) NEW_LINE root . left . right = newNode ( 2 ) NEW_LINE max_v = 0 NEW_LINE rootIndex = - 1 NEW_LINE findNodes ( root ) NEW_LINE print ( rootIndex ) NEW_LINE DEDENT