Find a value in BST
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.
Input Format:
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.
Constraints:
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
AnswerBot
1y
The task is to find if a given value is present in a Binary Search Tree (BST).
Start from the root node and compare the value with the target value.
If the value matches, return true.
If the target value...read more
CodingNinjas
author
2y
Easy approach, solved with log(n) complexity
CodingNinjas
author
2y
Recursion
As we know, a Binary Search Tree has the following properties:
- The left subtree of a node contains only nodes with values lesser than the node's value.
- The right subtree of a node contains onl...read more
Add answer anonymously...
Top Nvidia Software Developer interview questions & answers
Popular interview questions of Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app