Problem of the day
You have been given a Binary Search Tree and a key value ‘X’, find if a node with value ‘X’ is present in the BST or not.
Note:You may assume that duplicates do not exist in the tree.
For example :
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.
Output Format:
For each test case, return true if the given key value exists in the binary search tree else return false.
Note:
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
1
8 5 10 3 6 -1 14 -1 -1 -1 -1 13 -1 -1 -1
6
5 -1 -1
5
1
1
In test case 1, the tree after the construction is shown below.
Thus we can see that 6 is present in the tree.
In test case 2, there is only one node and we need to find the node with value 5 and thus it is present.
2
79 42 80 36 49 -1 90 -1 -1 -1 -1 85 100 -1 -1 -1 -1
49
710 340 800 210 390 -1 810 -1 -1 350 -1 -1 900 -1 -1 -1 -1
100
1
0
In test case 1, node value 49 is present in the tree hence output is 1.
In test case 2, node value 100 is not present in the tree so output is 0.
Make use of the properties of a Binary Search Tree.
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 :
O(N), Where ‘N’ is the number of nodes in the given BST.
Since if the binary search tree is left skewed or right skewed then the algorithm will make ‘N’ computations. Thus the time complexity will be O(N).
O(N), Where ‘N’ is the number of nodes in the given binary tree.
Since we are doing a recursive tree traversal and in the worst case (Skewed Trees), all nodes of the given tree can be stored in the call stack. Thus the overall space complexity will be O(N).