Check if value exists in level | Java 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
import java . util . * ; import java . io . * ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public static int findLevel ( Node root , int data ) { if ( data < root . data ) return - 1 ; if ( data == root . data ) return 0 ; int cur_level = 0 ; while ( true ) { cur_level ++ ; root = root . left ; if ( root . data == data ) return - 2 ; if ( root . data < data && ( root . left == null root . left . data > data ) ) { break ; } } return cur_level ; } public static int traversePath ( Node root , ArrayList < Integer > path ) { for ( int i = 0 ; i < path . size ( ) ; i ++ ) { int direction = path . get ( i ) ; if ( direction == 0 ) { if ( root . left == null ) return - 1 ; root = root . left ; } else { if ( root . right == null ) return - 1 ; root = root . right ; } } return root . data ; } static ArrayList < Integer > generateGray ( int n , int x ) { ArrayList < Integer > gray = new ArrayList < Integer > ( ) ; int i = 0 ; while ( x > 0 ) { gray . add ( x % 2 ) ; x = x / 2 ; i ++ ; } Collections . reverse ( gray ) ; for ( int j = 0 ; j < n - i ; j ++ ) gray . add ( 0 , 0 ) ; return gray ; } public static boolean binarySearch ( Node root , int start , int end , int data , int level ) { if ( end >= start ) { int mid = ( start + end ) / 2 ; ArrayList < Integer > encoding = generateGray ( level , mid ) ; int element_found = traversePath ( root , encoding ) ; if ( element_found == - 1 ) return binarySearch ( root , start , mid - 1 , data , level ) ; if ( element_found == data ) return true ; if ( element_found < data ) return binarySearch ( root , mid + 1 , end , data , level ) ; else return binarySearch ( root , start , mid - 1 , data , level ) ; } return false ; } public static boolean findKey ( Node root , int data ) { int level = findLevel ( root , data ) ; if ( level == - 1 ) return false ; if ( level == - 2 ) return true ; return binarySearch ( root , 0 , ( int ) Math . pow ( 2 , level ) - 1 , data , level ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 10 ) ; root . left . left = new Node ( 13 ) ; root . left . right = new Node ( 23 ) ; root . right . left = new Node ( 25 ) ; root . right . right = new Node ( 30 ) ; root . left . left . left = new Node ( 32 ) ; root . left . left . right = new Node ( 40 ) ; root . left . right . left = new Node ( 50 ) ; int arr [ ] = { 5 , 8 , 9 } ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( findKey ( root , arr [ i ] ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } }