Find the color of given node in an infinite binary tree | Function to find color of the node ; Maximum is to store maximum color ; Loop to check all the parent values to get maximum color ; Find the number into map and get maximum color ; Take the maximum color and assign into maximum variable ; Find parent index ; Return maximum color ; Function to build hash map with color ; To store color of each node ; For each number add a color number ; Assigning color ; Return hash map ; Driver code ; Build mapWithColor ; Print the maximum color
def findColor ( mapWithColor , query ) : NEW_LINE INDENT maximum = 0 NEW_LINE while ( query >= 1 ) : NEW_LINE INDENT if ( query ) in mapWithColor . keys ( ) : NEW_LINE INDENT maximum = max ( maximum , mapWithColor [ query ] ) NEW_LINE DEDENT if ( query % 2 == 1 ) : NEW_LINE INDENT query = ( query - 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT query = query // 2 NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT def buildMapWithColor ( arr , n ) : NEW_LINE INDENT mapWithColor = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mapWithColor [ arr [ i ] ] = i + 1 NEW_LINE DEDENT return mapWithColor NEW_LINE DEDENT arr = [ 3 , 2 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 7 NEW_LINE mapWithColor = buildMapWithColor ( arr , n ) NEW_LINE print ( findColor ( mapWithColor , k ) ) NEW_LINE