Check if value exists in level | Python3 implementation of the approach ; Class containing left and right child of current node and key value ; Function to locate which level to check for the existence of key . ; If the key is less than the root , it will certainly not exist in the tree because tree is level - order sorted ; If the key is equal to the root then simply return 0 ( zero 'th level) ; If the key is found in any leftmost element then simply return true No need for any extra searching ; If key lies between the root data and the left child 's data OR if key is greater  than root data and there is no level  underneath it, return the current level ; Function to traverse a binary encoded path and return the value encountered after traversal . ; Go left ; Incomplete path ; Go right ; Incomplete path ; Return the data at the node ; Function to generate gray code of corresponding binary number of integer i ; Create new arraylist to store the gray code ; Reverse the encoding till here ; Leftmost digits are filled with 0 ; Function to search the key in a particular level of the tree . ; Find the middle index ; Encode path from root to this index in the form of 0 s and 1 s where 0 means LEFT and 1 means RIGHT ; Traverse the path in the tree and check if the key is found ; If path is incomplete ; Check the left part of the level ; Check the right part of the level ; Check the left part of the level ; Key not found in that level ; Function that returns true if the key is found in the tree ; Find the level where the key may lie ; If level is - 1 then return false ; If level is - 2 i . e . key was found in any leftmost element then simply return true ; Apply binary search on the elements of that level ; Driver code ; Consider the following level order sorted tree 5 / \ 8 10 / \ / \ 13 23 25 30 / \ / 32 40 50 ; Keys to be searched
from sys import maxsize NEW_LINE from collections import deque NEW_LINE INT_MIN = - maxsize NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def findLevel ( root : Node , data : int ) -> int : NEW_LINE INDENT if ( data < root . data ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( data == root . data ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT cur_level = 0 NEW_LINE while True : NEW_LINE INDENT cur_level += 1 NEW_LINE root = root . left NEW_LINE if ( root . data == data ) : NEW_LINE INDENT return - 2 NEW_LINE DEDENT if ( root . data < data and ( root . left == None or root . left . data > data ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return cur_level NEW_LINE DEDENT def traversePath ( root : Node , path : list ) -> int : NEW_LINE INDENT for i in range ( len ( path ) ) : NEW_LINE INDENT direction = path [ i ] NEW_LINE if ( direction == 0 ) : NEW_LINE INDENT if ( root . left == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT root = root . left NEW_LINE DEDENT else : NEW_LINE INDENT if ( root . right == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT root = root . right NEW_LINE DEDENT DEDENT return root . data NEW_LINE DEDENT def generateGray ( n : int , x : int ) -> list : NEW_LINE INDENT gray = [ ] NEW_LINE i = 0 NEW_LINE while ( x > 0 ) : NEW_LINE INDENT gray . append ( x % 2 ) NEW_LINE x = x / 2 NEW_LINE i += 1 NEW_LINE DEDENT gray . reverse ( ) NEW_LINE for j in range ( n - i ) : NEW_LINE INDENT gray . insert ( 0 , gray [ 0 ] ) NEW_LINE DEDENT return gray NEW_LINE DEDENT def binarySearch ( root : Node , start : int , end : int , data : int , level : int ) -> bool : NEW_LINE INDENT if ( end >= start ) : NEW_LINE INDENT mid = ( start + end ) / 2 NEW_LINE encoding = generateGray ( level , mid ) NEW_LINE element_found = traversePath ( root , encoding ) NEW_LINE if ( element_found == - 1 ) : NEW_LINE INDENT return binarySearch ( root , start , mid - 1 , data , level ) NEW_LINE DEDENT if ( element_found == data ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( element_found < data ) : NEW_LINE INDENT return binarySearch ( root , mid + 1 , end , data , level ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( root , start , mid - 1 , data , level ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findKey ( root : Node , data : int ) -> bool : NEW_LINE INDENT level = findLevel ( root , data ) NEW_LINE if ( level == - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( level == - 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return binarySearch ( root , 0 , int ( pow ( 2 , level ) - 1 ) , data , level ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 5 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 10 ) NEW_LINE root . left . left = Node ( 13 ) NEW_LINE root . left . right = Node ( 23 ) NEW_LINE root . right . left = Node ( 25 ) NEW_LINE root . right . right = Node ( 30 ) NEW_LINE root . left . left . left = Node ( 32 ) NEW_LINE root . left . left . right = Node ( 40 ) NEW_LINE root . left . right . left = Node ( 50 ) NEW_LINE arr = [ 5 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( findKey ( root , arr [ i ] ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT