You may assume that duplicates do not exist in the tree.
For the given tree shown below:
For the binary tree shown in the figure, if ‘X’ = 6, the output will be 1 as node value 6 is present in the BST.
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
The second line of each test case contains an integer ‘X’ which denotes the key value to be searched in the binary search tree.
For each test case, return true if the given key value exists in the binary search tree else return false.
You don't need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 100
0 <= DATA <= 10^4
0 <= X <= 10^4
Where 'DATA' is the value of the binary tree node.
Time limit: 1 sec
As we know, a Binary Search Tree has the following properties:
Let findNode(TreeNode* <int> ‘ROOT’, int ‘KEY’) be a function which returns true if a node with value ‘KEY’ is present in the subtree rooted at the ‘ROOT’ node.
Now consider the following steps to implement the function :