Largest BST in Binary Tree

  Рет қаралды 89,925

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 92
@ShaliniNegi24
@ShaliniNegi24 4 жыл бұрын
I cannot frame the sentence to tell you how much I respect you! God bless you!
@Sushil2874
@Sushil2874 4 жыл бұрын
Tremendous explanation..!! I mean this guy makes everything so easy and simple..!! Thanks a lot Tushar..!!
@aneeinaec
@aneeinaec 3 жыл бұрын
This guy has ZERO drama... To the point allways..❤️
@suryac850
@suryac850 2 жыл бұрын
Thanks. Got this in an interview, but was stuck in trying to solve the problem. This video helped me understand the concept easily.
@NurcanSonmezcr
@NurcanSonmezcr 9 жыл бұрын
Thanks a lot Tushar! You have been really helpful!
@chetak2
@chetak2 8 жыл бұрын
Great!! I must have watched about 25 of your videos on different CS topics! Love it
@alexcomas2934
@alexcomas2934 5 жыл бұрын
Awesome video, just tried this on leetcode and your explanation helped me understand the problem more.
@03Karthikeyan
@03Karthikeyan 8 жыл бұрын
Thank you Tushar! I have been following your videos for over a year and have been spreading the word. Keep them coming.
@sasirekhamsvl9504
@sasirekhamsvl9504 3 жыл бұрын
Tu si great ho Tushar. No one can explain this better!!!!!!!!
@ErfanHossainShoaib
@ErfanHossainShoaib 9 жыл бұрын
Your all videos are simply excellent, awesome and too much helpful. Thanks
@hanusrikannan
@hanusrikannan 8 жыл бұрын
Excellent explanation. I went through your code in github for this problem. When either left subtree is not BST or right sub tree is not BST, shouldnt we have to set the size of BST as the size of subtree which is BST. If both sub tree's are not BST, then set the size of current sub tree as zero
@dharanyuvi6951
@dharanyuvi6951 3 жыл бұрын
Thanks alot Tushar and your code also very clean and self explanatory
@satya1067
@satya1067 3 жыл бұрын
Awesome Explanation sir🤓🤓
@ajaygaur3392
@ajaygaur3392 9 жыл бұрын
That elegant code, though. Thanks.
@leroyjamison5700
@leroyjamison5700 3 жыл бұрын
i know Im asking randomly but does anyone know a tool to log back into an instagram account? I was stupid lost my login password. I would love any help you can give me.
@kartermarcus3565
@kartermarcus3565 3 жыл бұрын
@Leroy Jamison instablaster :)
@leroyjamison5700
@leroyjamison5700 3 жыл бұрын
@Karter Marcus I really appreciate your reply. I found the site through google and im trying it out atm. Looks like it's gonna take a while so I will get back to you later when my account password hopefully is recovered.
@leroyjamison5700
@leroyjamison5700 3 жыл бұрын
@Karter Marcus it did the trick and I finally got access to my account again. I'm so happy:D Thank you so much, you saved my ass !
@kartermarcus3565
@kartermarcus3565 3 жыл бұрын
@Leroy Jamison No problem xD
@chandralekha7717
@chandralekha7717 6 жыл бұрын
Thanks for making trees a simple concept
@omkarsharan3967
@omkarsharan3967 7 жыл бұрын
as you told BST definition in starting of this video, 25 is the root of the tree and a leaf node also have a value 25 so I think its answer should not be 8.
@harshitajain323
@harshitajain323 7 жыл бұрын
your all videos are excellent am learning tree concepts from all your videos...just a request it well be great if you post lecture on binomial tree
@stith_pragya
@stith_pragya 10 ай бұрын
Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@bismitabhattacharjee4686
@bismitabhattacharjee4686 4 жыл бұрын
I would think node 19 returns (F,2,0,0) to node 18, not (F,1,0,0), correct me if I am wrong
@1gouravgg
@1gouravgg 9 жыл бұрын
thanks for your efforts Tushar. would you mind making some videos for backtracking programs.In spite of their utter importance in interview round there aren't any good tutorials available on net.
@SilveraDavid
@SilveraDavid 4 жыл бұрын
Thanks you, very clear!!!
@meghayall
@meghayall 9 жыл бұрын
How about a video on B Tree?
@jameslin9845
@jameslin9845 5 жыл бұрын
The explanation is easy to understand. Thanks.
@shiningyu9751
@shiningyu9751 8 жыл бұрын
Thank you for your crystal clear explanation! Very helpful!
@vm1662
@vm1662 4 жыл бұрын
Thanks a lot Tushar!
@cmaheshster1
@cmaheshster1 8 жыл бұрын
The BST can include only left or right and become a binary tree right?
@s1337m
@s1337m 8 жыл бұрын
Can you explain why the first approach is O(n^2)?
@nikkinic112
@nikkinic112 6 жыл бұрын
I can give a brief explanation. If you check each node individually if they are BST, you are repeating a lot of steps right? if you check the node and the whole tree if it is BST, you have to check if the left node is also a BST, and so forth. But when you check that the root is not a BST, then you have to check if the left node is also a BST again. So for each N node, you are doing as least 1....N more BST checking.
@SreyesSrinivasan
@SreyesSrinivasan 3 жыл бұрын
Great explanation, thank you so much!
@idontknow-wl6su
@idontknow-wl6su 3 ай бұрын
Thanks for a clear explanetion :D
@adis6867
@adis6867 5 жыл бұрын
Doing inorder traversal and finding the longest increasing subarray will work??
@vl5397
@vl5397 5 жыл бұрын
In this example, the longest increasing subarray in the inorder traversal is [20 25 35 40 50 55 60 70]. Size of the subarray is 8. So, yes, your suggestion seems to work. The post-order traversal in the video will also be able to give you the head of the BST. In the above array, the head node is not apparent. Edit: Perhaps, it can.
@adis6867
@adis6867 5 жыл бұрын
@@vl5397 i think it can be done even without using space, i mean do inorder traversal and keep track of previous visited node and keep comparing. If present node is greater than prev node increment counter else make it zero and keep track of max value of counter. Although i have not implemented but will try tomorrow.
@vl5397
@vl5397 5 жыл бұрын
@@adis6867 Yes, I think you have outlined the procedure well. On leetcode, this problem is premium, unable to try it out.
@adis6867
@adis6867 5 жыл бұрын
@@vl5397 i too tried but faced same issue, if you are able to find it on GitHub or other solution then please share, bcz this solution is good but i would not have solved it naturally of my own.
@jinka99
@jinka99 5 жыл бұрын
Thank you Tushar ! This has been very helpful ! thanks for making these videos
@kumarsrijan7566
@kumarsrijan7566 2 жыл бұрын
Tell you what this channel was ahead of it's time
@zhaodelong
@zhaodelong 8 жыл бұрын
when child node is null, the min / max of the parent should be parent.val, not 0. if it's 0, it will fail the Leetcode OJ
@MohitG7701
@MohitG7701 3 жыл бұрын
awesome explanation sir...this video of yours is truly helpful....thanks a lot sir for making this video
@ditibagga9255
@ditibagga9255 7 жыл бұрын
If we find inorder at each root, and see which is the longest sorted one, won't that solve the problem?
@shivansh-gup
@shivansh-gup 2 жыл бұрын
no because BST must include leaf nodes and longest sorted one in inorder array does not guarantee this
@SBlogIndia
@SBlogIndia 4 жыл бұрын
The explanation is good, but most I liked the git hub code
@mjdatt
@mjdatt 9 жыл бұрын
Thanks Tushar!! That was really helpful.
@koustavchatterjee2907
@koustavchatterjee2907 9 жыл бұрын
Hey Tushar, can you publish some videos on suffix array creation algorithm ?
@koustavchatterjee2907
@koustavchatterjee2907 9 жыл бұрын
Thanks buddy
@koustavchatterjee2907
@koustavchatterjee2907 9 жыл бұрын
***** tushar, it will be great if it is a O(n) algo
@dronelektron
@dronelektron 9 жыл бұрын
Koustav Chatterjee and suffix tree (Ukkonen) :3
@dronelektron
@dronelektron 9 жыл бұрын
Waiting :3
@subashsah4831
@subashsah4831 9 жыл бұрын
Its a request plz also upload the code for all your videos in c or c++
@anuragdani9739
@anuragdani9739 8 жыл бұрын
A very good explanation...!
@harsha-ix9le
@harsha-ix9le 8 жыл бұрын
Hi tushar, .. .50 .../ ........ \ ..30........ 60 ../.....\...... /.. \ .5.... 20.... 55...70 ..................... /.....\ .....................65 80 Cant we perform inorder traversal.and keep count like for suppose intialize max count=0; count=1; 1.5
@harsha-ix9le
@harsha-ix9le 8 жыл бұрын
+Tushar Roy 10 / 1 is a BST only right?
@kartikchauhan5498
@kartikchauhan5498 7 жыл бұрын
Good guy Tushar
@YogeshGuptaiamyogeshg
@YogeshGuptaiamyogeshg 8 жыл бұрын
What if we traverse inorder and find maximum patch of elements which are in sorted order..
@YogeshGuptaiamyogeshg
@YogeshGuptaiamyogeshg 8 жыл бұрын
yes i realise, thanks.
@pymondo1147
@pymondo1147 6 жыл бұрын
1 lakh subcriptions ..You deserve iit sir :):) Thank you for your efforts:)
@lifehacks9450
@lifehacks9450 4 жыл бұрын
this is an amazing concept
@parveshsingh6611
@parveshsingh6611 7 жыл бұрын
@Tushar roy Logic is fine but you should also explain the code.
@varshapatidar7239
@varshapatidar7239 5 жыл бұрын
very helpful ! Thanks..
@swagatpatra2139
@swagatpatra2139 4 жыл бұрын
good job Sir
@HemanthKumar-od2ej
@HemanthKumar-od2ej 8 жыл бұрын
superb explanation! :)
@pennapatipavan5846
@pennapatipavan5846 4 жыл бұрын
Awesome bro. Thanks for such good explaination
@namratakhandelwal8143
@namratakhandelwal8143 7 жыл бұрын
very helpful !
@spk9434
@spk9434 5 жыл бұрын
Excellent !!
@quantlfc
@quantlfc 2 жыл бұрын
Awesome.
@rajeshd7389
@rajeshd7389 7 жыл бұрын
Nice Explanation !!
@syedtahamohiuddin3160
@syedtahamohiuddin3160 4 жыл бұрын
excellent !!!
@1388tushr
@1388tushr 7 жыл бұрын
Hi Tushar, the explanation was simple and easy to understand. I think instead of using postorder traversal, easier way out is to do an inorder traversal on the tree and store the output of inorder node values in an array. Once this is done just find out the maximum increasing subsequence in the array. This will give you the size of the largest BST in a binary tree.
@shankardaruga3264
@shankardaruga3264 7 жыл бұрын
Yes. That should be the correct sub Tree. Here We are not considering all the possible sub Trees. Here we are only checking sub trees that traverse up till the leaf and also, the left and right should be sub trees to be considered.
@kartikchauhan5498
@kartikchauhan5498 7 жыл бұрын
No, your solution won't work for all the cases. Take this case for example- 5 / \ 4 6 / \ 25 2 ur logic's ans : size 3 (4 5 6) original ans : size 1
@xxbighotshotxx
@xxbighotshotxx 5 жыл бұрын
@@kartikchauhan5498 You're correct. Thanks for the counterexample
@stephyjacob1256
@stephyjacob1256 7 жыл бұрын
Thanks Gautam gambhir. Oh sh*t. ..I mean Tushar . :p
@arjoojain9809
@arjoojain9809 6 жыл бұрын
Awesome!
@azharmarmu19
@azharmarmu19 9 жыл бұрын
Thanks a lot bro...
@KUSHUTRIVEDI
@KUSHUTRIVEDI 9 жыл бұрын
Hello Sir, I have question - If we do not include 19 and 15 in left subtree of 25 and 20 and 25 on right subtree, that would still be a BST. Size would be 11. Why did not count 18(root -> left) and 25(root), in our largest BST? Thank you in advance.
@KUSHUTRIVEDI
@KUSHUTRIVEDI 9 жыл бұрын
***** Thank you sir..!
@shivpratapsingh7992
@shivpratapsingh7992 8 жыл бұрын
it was amzing! KEEP IT UP
@ayushmantiwari7547
@ayushmantiwari7547 3 жыл бұрын
//Suggest improvement if any..BTW giving correct output...in C++ //largest BST from BT #include using namespace std; struct Node { int data; Node *left; Node *right; Node(int val) { data = val; left = NULL; right = NULL; } }; struct Data { int mini; int maxi; int size; bool isbst; }; Data largestBSTinBT(Node *root) { if (root == NULL) { return {INT_MAX, INT_MIN, 0, true}; } if (root->left == NULL && root->right == NULL) { return {root->data, root->data, 1, true}; } Data Left = largestBSTinBT(root->left); Data Right = largestBSTinBT(root->right); Data curr; if (root->data < Right.mini && root->data > Left.maxi && Left.isbst && Right.isbst) { curr.size = Left.size + Right.size + 1; if(Left.mini!=INT_MAX) curr.mini = Left.mini; else curr.mini=root->data; if(Right.maxi!=INT_MIN) curr.maxi = Right.maxi; else curr.maxi=root->data; curr.isbst = true; return curr; } else { curr.size = max(Left.size, Right.size); curr.mini = 0; curr.maxi = 0; curr.isbst = false; return curr; } } int main() { Node *root = NULL; root = new Node(25); root->left = new Node(18); root->right = new Node(50); root->left->left = new Node(19); root->left->right = new Node(20); root->left->left->right = new Node(15); root->left->right->left = new Node(18); root->left->right->right = new Node(25); root->right->left = new Node(35); root->right->right = new Node(60); root->right->right->left = new Node(55); root->right->right->right = new Node(70); root->right->left->right = new Node(40); root->right->left->left = new Node(20); root->right->left->left->right = new Node(25); Data S = largestBSTinBT(root); cout
@sinboowang8063
@sinboowang8063 4 жыл бұрын
coooooooooool
@sunilnk95
@sunilnk95 9 жыл бұрын
good work :)
@madhukiranattivilli2321
@madhukiranattivilli2321 3 жыл бұрын
But there are 2 ways to determine largest bst in a bt (1) what tushar did -- if the subtree with node X as root is a BST, but is not a BST when associated with X's parent, DISASSOCIATE WITH PARENT and the subtree with X as root is one of the contenders for LARGEST BST. Here Tushar used POSTORDER TRAVERSAL (2) another way -- if the subtree with node X as leaf is a BST, but is not a BST when associated with X's child, DISASSOCIATE WITH CHILD AND MAKE X AS A BST LEAF. Here we can use the algo "Is BT a BST", wherein if (a, b) are (minval, maxval) for node X, (a, X) and (X, b) would be the (minval, maxval) for X's left and right children respectively. Wherever this assertion fails, that child of X would be made NULL, thus disassociating from the BT to maintain it as a BST. :) Please comment
@saifurrahmanbhuiyan925
@saifurrahmanbhuiyan925 9 жыл бұрын
Brother we need Recursive Backtracking
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 146 М.
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 262 #shorts
00:20
Flipping Robot vs Heavier And Heavier Objects
00:34
Mark Rober
Рет қаралды 59 МЛН
Check if Binary Tree is Binary Search Tree
10:29
Tushar Roy - Coding Made Simple
Рет қаралды 122 М.
Maximum Size Rectangle of All 1's Dynamic Programming
6:55
Tushar Roy - Coding Made Simple
Рет қаралды 181 М.
System design : Design Autocomplete or Typeahead Suggestions for Google search
19:42
Tushar Roy - Coding Made Simple
Рет қаралды 310 М.
Skyline Problem
22:54
Tushar Roy - Coding Made Simple
Рет қаралды 193 М.
Count Number of Binary Search Tree Possible given n keys Dynamic Programming
11:00
Tushar Roy - Coding Made Simple
Рет қаралды 85 М.
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 116 М.
Level by Level Printing of Binary Tree
15:37
Tushar Roy - Coding Made Simple
Рет қаралды 66 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 262 #shorts
00:20