L40. Search in a Binary Search Tree | BST | C++ | Java

preview_player
Показать описание


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

Рекомендации по теме
Комментарии
Автор

Hi, hope you are well.


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL and many other time saving features under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing yourself.


takeUforward
Автор

My approach :
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL) return NULL;
if (root->val == val) return root;
if (val < root->val) return searchBST(root->left, val);
else return searchBST(root->right, val);
}

pritishpattnaik
Автор

Almost about to finish free ka tree series... :) Thank you so much for such amazing content.
Your hard work is appreciable. After watching a video I always try to explain in same way how you used to explain intuition and dry run each step. It helps me a lot and also increases my code quality by Looking at your code. 🙌

aiartisry
Автор

This is best series on Trees I have ever come across on youtube. Striver salute to your passion and commitment to teaching people

satyapraneethkatta
Автор

bhai 1 no.content hai aur samjhaya bhut acche se hai ....bss abhi paid mt abhi tree aadha hi pda hai aur graph poora baaki hai

Anand-zgjv
Автор

Thank you so much for BST !! Improving with every video.

shivangisrivastava
Автор

BST can also have depth N(if tree is skwed) it's no always Logn height.

MandeepKumar-uezy
Автор

class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(!root) return NULL;
if(root->val==val) return root;
if(root->left && val<root->val) return searchBST(root->left, val);
if(root->right && val>root->val) return searchBST(root->right, val);
return NULL;
}
};

shaikfahim
Автор

thank you so much Vi 🙂I was almost to complete my tree series, the way of explanation i loved it a lot.

niharika_
Автор

2 Line Java Recursive Solution Based on Same Approach :
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val) return root;
return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val);
}

spandanrastogi
Автор

yes recursive solution is also easy and possible in case u are thinking

iamnottech
Автор

sir 10 can also lie on the lesser part -> the point is to make the right node greater than the root's value so we can make the right node of any node 10, that is how the lesser part will have the greater node . Please somehow reply if i go somewhere wrong

VivekSharma-ehtv
Автор

I think one correction should be there.

The traversal time will not be log(N) for almost all cases <- It is true for all Balanced Binary Search Trees, But normal Binary Search Trees can be like.
[1, null, 2, null, 3, null, 4, null, 5, null, 6] <- use leetcode's testcase area to visualize this.

Basically this is also a Binary Search Tree by definition but here to find 6 you will need to go thorugh all the elements, making this traversal O(N).

So worst case traversal of BST is still O(N), But for balanced BST the traversal is O(log(N)).

Nikhil-Tomar
Автор

Bro you are doing superb work, very very thanks

vaibhavpoliwal
Автор

Please likeeee, shareeee and :) Also follow me at Insta: Striver_79

takeUforward
Автор

what about if we are given a linked list wont it be O(N) time comp ? like flatten binary tree Q we had all roots in right

enigmanarratives
Автор

Could someone please explain to me the difference return searchBST(root.left, val); and searchBST(root.left, val) ?
Thanks in advance

madetolaugh
Автор

Bro, is it preferred to use conditional operator in interviews instead of if else? Bcz the code looks complex using conditional operator.

shouvikdatta
Автор

If bool is to be returned instead of pointer, how can that be done ?

abhijitkrao
Автор

Could anyone please tell me where the articles from the sheet are??

herculean
visit shbcf.ru