2.Search in a Binary Search Tree
Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]Input: root = [4,2,7,1,3], val = 5
Output: []Solution Iteratively:-
class Solution
{
public:
TreeNode *searchBST(TreeNode *root, int val)
{
TreeNode *t = root;
while(t!=NULL){
if(t->val == val){
break;
}
else if(t->val > val){
t = t->left;
}
else{
t = t->right;
}
}
return t;
}
};Solution Recursively:-
Time Complexity of searching in a BST
Last updated

