L53. Largest BST in Binary Tree

  Рет қаралды 159,381

take U forward

take U forward

Күн бұрын

Пікірлер: 332
@takeUforward
@takeUforward 2 жыл бұрын
This is the last video of this series. I hope you enjoyed this. Just a small request: “Please share the series, also you can tag me at Linkedin if you want a give a review for this series, this will do me a world of good” Thankyou 🙏🏻
@yash0270
@yash0270 2 жыл бұрын
Thanks for creating this series.
@akhilesh59
@akhilesh59 2 жыл бұрын
Thank you Bhaiya for this Wonderful series. It was the best DSA based series for me so far. I have already recommended this to all my friends. Now going to start the Graph series 😁..
@sejalrai7358
@sejalrai7358 2 жыл бұрын
completely Lovedd this seriess!! thanks a lotttt
@krishnarajs929
@krishnarajs929 2 жыл бұрын
Thanks bro , It was absolutely great...
@vaibhavkumar526
@vaibhavkumar526 2 жыл бұрын
Hi I feel like the base case value is incorrect, lets say when root is Null, then my tuple returned should have maxNode as -infinity and minNode as +infinity. Then only lets say for a leaf node we can have a value of 1, as a single node is also a BST. Please check this. I tried dry running the code and it didn't work, so I thought I should share this. Well I might be wrong, so please cross verify. I can prove this, lets say, the leaf node is 10, so from the left side the max value would be -infinity and from the right side min value would be +infinity and hence the condition -infinity < 10 and 10 < + infinity would satisfy
@subodh.r4835
@subodh.r4835 2 жыл бұрын
CONGRATS to everyone else who successfully completed this playlist!
@kushagraahire1871
@kushagraahire1871 Жыл бұрын
Did you get a job after learning DSA?
@ultimategyani6412
@ultimategyani6412 Жыл бұрын
​@@kushagraahire1871 yes I got a full stack developer internship in Lockheed Martin( remote ). They asked me two DSA ques in technical round Print spiral matrix Lowest common subsecuence using DP
@kushagraahire1871
@kushagraahire1871 Жыл бұрын
@@ultimategyani6412 congratulations
@ultimategyani6412
@ultimategyani6412 Жыл бұрын
@@kushagraahire1871 thanks brother
@horccruxxxop4184
@horccruxxxop4184 Жыл бұрын
@@ultimategyani6412 what were your projects?
@arpanbanejee5143
@arpanbanejee5143 2 жыл бұрын
Completed the entire FREE KA TREE series! Learnt a lot! Thank you Striver for putting up such quality content!
@bhargav1811
@bhargav1811 Жыл бұрын
11:27 Minimal will be 30 and not 40 !! Thank you striver for this series!! Helped me alot !!
@Area-fd8ht
@Area-fd8ht Жыл бұрын
​@@namashsharma7550tum toh lodu ho fir..😂
@PrinceTripathi-j7u
@PrinceTripathi-j7u 8 ай бұрын
Completed the Tree series in 10 days. Thank you STRIVER for making such a quality content .
@abdulrazzak2934
@abdulrazzak2934 2 жыл бұрын
finally completed this amazing free ka tree series and now im taking a break then soon i'll start the next your graph series thanks alot striver for amazing explanation,efficient code ,time complexity an all.thank you so much
@vaishnavigour5777
@vaishnavigour5777 2 жыл бұрын
completed this series today! THANKYOU STRIVER for making such a quality content . Really helped a lot . It took me 6 days to complete this series . It was worth it.
@kartiksrivastava3897
@kartiksrivastava3897 2 жыл бұрын
currently watching the last video it took me 14 days.
@codding32world50
@codding32world50 2 жыл бұрын
like how many videos per day
@parthsalat
@parthsalat 2 жыл бұрын
Did you save/write the codes somewhere, like in a notebook or notion? Because storing codes takes up a lot of my time.
@vaishnavigour5777
@vaishnavigour5777 Жыл бұрын
Yes @parth I have noted them down in a notebook...not the full code but just the pseudo code
@parthsalat
@parthsalat Жыл бұрын
@@vaishnavigour5777 Great! After consulting my "smart" friends, I too have decided to save codes only for difficult questions. For normal questions, I'll just jolt them down in a paper.
@shobhitshrivastav9775
@shobhitshrivastav9775 2 жыл бұрын
Today I have completed the Free Ka Tree series playlist .It took almost 21 days to complete this series from start to end .I really enjoyed the playlist and learn many new ways of solving tree questions ...thanku striver bhaiya for this top quality content always be thankful to you❤️❤️
@sujoyseal195
@sujoyseal195 2 жыл бұрын
Correction : At 11:30 , the minimum for subtree with root=40 is 30 not 40 . Please provide an edit at this juncture
@abhimanyuraizada7713
@abhimanyuraizada7713 2 жыл бұрын
It made my head spin
@interesting.finds.1857
@interesting.finds.1857 2 жыл бұрын
true;
@Now_I_am_all_fired_up
@Now_I_am_all_fired_up 2 жыл бұрын
True Bhai yrr mera dimag kharab ho gya .......
@vishichaurasiya4427
@vishichaurasiya4427 Жыл бұрын
@@Now_I_am_all_fired_up L lag gaye the mere 😓
@piyushlohiya1977
@piyushlohiya1977 Жыл бұрын
searching for this comment
@aaqilkrishna8840
@aaqilkrishna8840 Жыл бұрын
Just completed this playlist and I would highly recommend it to anyone wanting to learn how to solve Binary Tree and Binary Search Tree based problems from scratch. Thank you for this valuable content !! Cheers ✌❤
@AkashDeep-jp1ts
@AkashDeep-jp1ts 4 ай бұрын
The hardest tree question I came across but thanks to you, now it's completely clear!!
@noone-pn7yu
@noone-pn7yu 9 ай бұрын
completed whole playlist today, thank striver
@taranjotsingh7780
@taranjotsingh7780 Жыл бұрын
Completed the entire series today. Feeling much more confident now. Thank you very much Stiver! 😃
@Dontpushyour_luck
@Dontpushyour_luck 11 ай бұрын
completed the entire tree series. Enjoyed a lot! thank you for all the content striver!
@captcha14
@captcha14 Жыл бұрын
Earlier trees and graphs haunt me. Completed this playlist and now questions are solvable. Thank you striver.
@closetoheart6120
@closetoheart6120 Жыл бұрын
bhai are these playlist to understand concept easily ?? and is whole DSA complete here ?
@pushkarchoudhary8276
@pushkarchoudhary8276 Жыл бұрын
@@closetoheart6120 tree playlist are awesome u can follow this according to me
@aadityakhetan956
@aadityakhetan956 2 жыл бұрын
A great series to learn tree from scratch. Thanks for top notch content. It was really very helpful.
@Tahirkhan-hv3jn
@Tahirkhan-hv3jn Жыл бұрын
**important** if someone is confused while doing dry runwith the solution and example then consider this, There is issue in naming of variables in the code, even though things work well. The maxNode should be replaced by minNode and vice-versa.
@lifegood6401
@lifegood6401 2 жыл бұрын
It is such a Great Series. More power to Striver. You are the superhero of students like us.
@_CSE_PRATHISWARANGR
@_CSE_PRATHISWARANGR 10 ай бұрын
Just completed your amazing tree series Striver and thanks for making such a wonderful series for free :)
@ashishpradhan6250
@ashishpradhan6250 Ай бұрын
another playlist completed.. thanks for bringing a change in other's lives
@jagjiwanchimkar8106
@jagjiwanchimkar8106 2 жыл бұрын
Mistake at 11:28 , For 40, smallest will be 30 not 40
@ashoksrinivasan4627
@ashoksrinivasan4627 2 жыл бұрын
S
@rishabhgupta1222
@rishabhgupta1222 3 ай бұрын
Just completed the Tree series playlist....absolutely mindblowing content with highest level of perfection !!!!
@iamnoob7593
@iamnoob7593 7 ай бұрын
Done with free ka tree series. A big thank you striver , I taught i knew trees very well , But when i watched these videos , I had not known most of it.
@divyamgupta-t3i
@divyamgupta-t3i 10 ай бұрын
Can this approach work, not wrt TC, but the intuition only ? 1. Find Inorder of binary tree and store it in vector 2. Find the largest sorted substring in that vector Considering each element in tree is unique.
@selvamani.kannan
@selvamani.kannan 6 ай бұрын
yes, it should work. length of longest increasing sub array
@SameerRehan-tt4lh
@SameerRehan-tt4lh 4 ай бұрын
no
@dank7044
@dank7044 4 ай бұрын
It won't work,because we need to return a node as an ans. so the ans would be the complete tree,originating from that node,you cant selectivel keep or exclude any of its child,by your method, you might do this
@ashutoshpandey904
@ashutoshpandey904 2 жыл бұрын
Finally, Completed this entire series and learnt a lot! It took me around 3 weeks to complete this series and it's totally worth it. Thank you Striver for such quality content, Really helped me a lot !❤❤
@Manishgupta200
@Manishgupta200 11 ай бұрын
This series is superb. Understood all series problem with proper Dry run and solved in multiple ways from Brute to Better to Optimal. Apart from that solved logical tree problem by myself.. Digonal traversal, Path Sum, Sum tree.. and going on..
@tejassrivastava6971
@tejassrivastava6971 2 жыл бұрын
solution with cam makes more easier to understand the concept. Thanks!
@arghya_0802
@arghya_0802 2 жыл бұрын
Completed the entire Tree Series. The best one without a doubt!! Finished all the questions & will probably do a couple more. The next destination is DP !!
@priyanshkumar17
@priyanshkumar17 7 ай бұрын
This is one of the best videos of the playlist !!! Thanks Striver bhaiya :)) You made trees easier to understand & visualize...
@FTWaditya
@FTWaditya 4 ай бұрын
**Correctionss if(root==NULL) return {INT_MIN,INT_MAX,0}; if(root->data>left.mx && root->datadata,right.mx),min(root->data,left.mn),1+right.n+left.n}; } else { return {INT_MAX,INT_MIN,max(left.n,right.n)}; }
@mohammedraiyaan-bt6iy
@mohammedraiyaan-bt6iy Ай бұрын
Thanks sir , Because of you another topic of dsa is completed.
@____Akash__
@____Akash__ Жыл бұрын
Time Complexity O(N^2) and sc O(1) class Solution{ public: //the brute force approach is first i'll check for every node if this node // is a root of a bst or not if this node contains bst then we cnt the size //time complexity O(N^2) bool isValidBST(Node* root,int min, int max){ if(root == NULL) return true; if(root->data < min || root->data > max){ return false; } return isValidBST(root->left, min, root->data) && isValidBST(root->right, root->data, max); } int countNode(Node* root){ if(root == NULL) return 0; if(!root->left && !root->right) return 1; int left = countNode(root->left); int right = countNode(root->right); return left + right + 1; } void checkNode(Node* root, int &maxi){ if(root == NULL) return; if(!root->left && !root->right) return; if(isValidBST(root,INT_MIN,INT_MAX) == true){ int cnt = countNode(root); maxi = max(cnt, maxi); } checkNode(root->left, maxi); checkNode(root->right, maxi); } int largestBst(Node *root) { int maxi = 1; checkNode(root, maxi); return maxi; } };
@saurabhgautam2697
@saurabhgautam2697 2 жыл бұрын
Completed the whole series ,I was kindaa slow ,took me 3 weeks-1 month,but understood the concepts very well ,as you always say straight into the mind Thankyou bhaiya
@AngadSingh97
@AngadSingh97 2 жыл бұрын
Hi everyone The link from the SDE SHEET links to Maximum Sum BST in Binary Tree, whereas Striver has solved for Largest BST in Binary Tree. I did not read the question description correctly and was using the approach for the wrong question. Please be careful so that you don't waste 30 minutes like I did, debugging the code and wondering why it's not working. Good to reach the end of Tree Series, thank you Striver bhai. Now on to Graph and DP Problems : ).
@HEMANTPORWAL-t7d
@HEMANTPORWAL-t7d 2 ай бұрын
495k to 137k itself speaks many things well done striver bhaiya
@madhu_mohanreddy
@madhu_mohanreddy 2 ай бұрын
630k*
@pavannettam8968
@pavannettam8968 Жыл бұрын
The playlist was really helpful to understand trees, thank you so much @striver. I'm sure this is the best tree playlist in the market.
@fakegmail3051
@fakegmail3051 Жыл бұрын
code is not working bro
@gouravkushwaha3001
@gouravkushwaha3001 11 ай бұрын
Completed this amazing free ka tree series today
@abhijeetanand3443
@abhijeetanand3443 2 жыл бұрын
woho finally reached here, thankyou so much striver (Sir/Bhaiya) for this playlist. Many will come through all videos till here and each time you will be blessed by tons 🙌🤩
@tanmaychaudhary2801
@tanmaychaudhary2801 Жыл бұрын
In starting i have got fear from tree topic...and vertical order traversal topic was demotivating for me because at that time I was thinking this(tree and DSA) is not for me....but I continued it and striver bhaiya made it very easy to understand all the topics and now I am feeling very much familiar with this topic...now I am able to think for solutions by myself....thanks a lot striver bhaiya...❣️💫🙌😊🙏
@LBK3
@LBK3 Жыл бұрын
Whole tree series is absolutely amazing used to have a lot of confusion on trees and pointers used in them... Completely cleared thanks alott Striver❤
@sunnykumarsingh7039
@sunnykumarsingh7039 2 жыл бұрын
Today complete the SDE sheet. This was the last question I attempted. Yeah, the order in which I did was according to my comfort, Nevertheless, your content is LIFE-SAVER!!
@ruturaj_dm
@ruturaj_dm 9 ай бұрын
I m not sure how is this solution a O(1) space complexity. We are creating and returning NodeValue object from each node. So for all the O(N) nodes we are returning a new object. If every object is assumed to take O(1) space then we have O(N)*O(1) = O(N) space.
@pqrstwxyz1175
@pqrstwxyz1175 8 ай бұрын
Same doubt...did you get any explanation?...surely has to be O(N)
@deepaksaini3986
@deepaksaini3986 2 жыл бұрын
Completed successfully!! Now revision is the only thing to do !! and will do it!!
@parthsalat
@parthsalat 2 жыл бұрын
Thanks for the series...I'd have never learnt BST if it wasn't for this series
@varnit1975
@varnit1975 2 жыл бұрын
At size 4 the minimal should be min(40,30) = 30 not 40
@HarshKumar-ip5nr
@HarshKumar-ip5nr Жыл бұрын
Just an approach. What if we do inorder traversal and find the index of points that violate the increasing vector. Now, we have simultaneously calculated the size of each seperate BST in inorder traversal and return the max one. i.e. suppose the iteration was like: 2, 3, 4, 5, 1, | 6, 7, 8, 10, 12, 13, 9 | 20, 21.. The sizes are : 5, 7, 2 and we return 7. Any thoughts and code?
@sarthakragwan5248
@sarthakragwan5248 Жыл бұрын
This approch fails i also did this one
@HarshKumar-ip5nr
@HarshKumar-ip5nr Жыл бұрын
@@sarthakragwan5248 why, can you give some insight
@dank7044
@dank7044 4 ай бұрын
@@HarshKumar-ip5nr It won't work,because we need to return a node as an ans. so the ans would be the complete tree,originating from that node,you cant selectivel keep or exclude any of its child,by your method, you might do this
@arpitagupta5834
@arpitagupta5834 2 жыл бұрын
In the first example, why isn't 10 included in the BST? (10,5,1,8)
@Nikita-hv5jm
@Nikita-hv5jm 2 жыл бұрын
finallyy completed the whole series....thanks a lot striver for such a wonderful series on tree...💫
@vaibhav5269
@vaibhav5269 6 ай бұрын
Hi Striver, Thanks for the free tutorials. I had a small doubt in this problem: If we just exclude 17 from the tree , the remaining tree is a BST, then the size would become 10. Why dont we solve it in this way, It is also a subtree right?
@momilijaz271
@momilijaz271 2 жыл бұрын
I am in love with this amzing content, definitely gonna follow all other playlists and striver's sheet. Can;t thank you enough for the great content.!
@gouravkumar7459
@gouravkumar7459 2 жыл бұрын
Thanks for this amazing playlist, though I knew trees and have done a lot of questions on it, still I wanted to learn new approaches, indeed I learned amazing approaches. Thanks again. :)
@asit7456
@asit7456 2 жыл бұрын
Could you please give notes of the tree series it is incomplete as the notes and the effective explanation makes the series effective.
@rishabhgupta9846
@rishabhgupta9846 Жыл бұрын
Completed the series in 2 weeks ,thank you Striver for such wonderful playlist.
@anshumaan1024
@anshumaan1024 Жыл бұрын
I took 3-4 weeks 🙂
@prateek4-yearb.tech.chemic511
@prateek4-yearb.tech.chemic511 Жыл бұрын
I CAN CONFIDENTLY SAY THAT THIS IS THE ABSOLUTE BEST SERIES ON KZbin FIR TREES; LAST QUESTIONS WERE JUST TOP NOTCH . LOVED IT THANKS STRIVER .
@MdArman_cs
@MdArman_cs 10 ай бұрын
Finally i successfully completed the free ka tree series thanks striver bhaiya🧡
@shirishprataya4602
@shirishprataya4602 2 жыл бұрын
Finally completed this series. Thanks a lot Striver. You have helped a lot. This truly was the best tutorial on trees. 🔥🔥🔥
@OmPrakash-kb2wq
@OmPrakash-kb2wq 2 жыл бұрын
Thank you so much striver. I learnt a lot from this series. Earlier I have very low confidence in Tree and now after your series and practice I leave become very much confident in this portion.
@gsmdfaheem
@gsmdfaheem 2 жыл бұрын
Finally completed the entire Free Ka Tree Series....Thank you so much striver bhaiya for putting in all the efforts...You are the best.
@sautramanivibhuti4597
@sautramanivibhuti4597 10 ай бұрын
Finally completed Thanks striver bhaiya You owe my respect 📈❤️
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
Completed this free ka series today in 4.5 days and it was amazing. Thank you striver bhaiya for providing this amazing content for free. :)
@mohammedsaqib1250
@mohammedsaqib1250 2 жыл бұрын
Itni jaldi 🥺
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
@@mohammedsaqib1250 Yes as placement season is coming 😄
@mohammedsaqib1250
@mohammedsaqib1250 2 жыл бұрын
@@gandhijainamgunvantkumar6783 where r u from
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
@@kms8320 Wow.. Amazing 👏👏
@ayushdhiman8128
@ayushdhiman8128 2 жыл бұрын
ky ye series yaha samaapt hoti hai? Thank you for this free playlist ❤
@huungryyyy
@huungryyyy 2 ай бұрын
bhai kitna pyaara soln likha hai 😍
@AyushiSingh-iy5uf
@AyushiSingh-iy5uf Жыл бұрын
Merge two BSTs is missing in this series, please upload that too! Thanks in advance :)
@stith_pragya
@stith_pragya 10 ай бұрын
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@navyaposham-hx6ix
@navyaposham-hx6ix 3 ай бұрын
I dont understand we can just exclude 14&17 right? If we include 15,16,18,19,20,30,40,50,60 we get a bst of size 9 and that can be our answer
@abhinavpandey3356
@abhinavpandey3356 2 жыл бұрын
Is the complexity of brute is o(n^3) ie for passing every node to validate * validating *getting size if it is a valid BST ie o(n*n*n) ?please check
@nadeemqwerty
@nadeemqwerty 2 жыл бұрын
I am not sure but it seems to be wrong, what if subtree is not BST but follows the min max condition?
@tusharnain6652
@tusharnain6652 2 жыл бұрын
d*mb
@anmolgarg2951
@anmolgarg2951 2 жыл бұрын
we are moving from bottom to top so every bottom node also following the MIN MAX condition so it must be BST as we are changing MAX to INT_MAX and MIN to INT_MIN whenever a single node is not satisfying the MIN MAX condition.. so it must be BST if it follows MIN MAX condition..
@abhirupbasu3386
@abhirupbasu3386 Жыл бұрын
Completed the tree series.Thanks for making this amazing tree series.
@mdrehankhan3458
@mdrehankhan3458 Жыл бұрын
Thanks man!! this series really helped me to solve tree problems which I found quite difficult while l solving tree questions.
@DSRhymster
@DSRhymster Жыл бұрын
Completed the whole tree series today.... Learnt alot thank you Striver the saviour 🥰🥰
@sarthakkapoor3764
@sarthakkapoor3764 2 жыл бұрын
How many more episodes left in the tree series?
@shantipriya370
@shantipriya370 8 ай бұрын
completed the list.. thank you so much..
@ArBond007
@ArBond007 2 жыл бұрын
Completed the playlist in 10 days, was an amazing journey...
@mohdhammadsiddiqui7598
@mohdhammadsiddiqui7598 2 жыл бұрын
With questions or without questions???
@ArBond007
@ArBond007 2 жыл бұрын
@@mohdhammadsiddiqui7598 with questions
@mohdhammadsiddiqui7598
@mohdhammadsiddiqui7598 2 жыл бұрын
@@ArBond007 I'm currently on 35th video after completing playlist will start
@ArBond007
@ArBond007 2 жыл бұрын
@@mohdhammadsiddiqui7598 nice
@sanginigupta1312
@sanginigupta1312 2 жыл бұрын
++
@kashishsharma6809
@kashishsharma6809 2 жыл бұрын
i like brute force also, putting memorization for sub branches.
@sulakshsharma8520
@sulakshsharma8520 9 ай бұрын
amazing explanation, thanks!
@vivekchatterjee4001
@vivekchatterjee4001 2 жыл бұрын
Completed the Series today, developed logic, and made notes, next is the Graph series. Journey never ends✌✌
@dakshkalucha5408
@dakshkalucha5408 Жыл бұрын
There is issue in naming of variables in the code, even though things work well. The maxVal should be replaced by minVal and vice-versa.
@bhushanasutkar6135
@bhushanasutkar6135 Жыл бұрын
true
@ayobrrr
@ayobrrr 2 ай бұрын
class Solution: def largestBst(self, root): def rec(node): if node: if not node.left and not node.right: return [True, node.data, node.data, 1] left = rec(node.left) right = rec(node.right) if left[0] and right[0] and left[2] < node.data < right[1]: return [True, min(left[1], node.data), max(right[2], node.data), left[3] + right[3] + 1] return [False, -1, -1, max(left[3], right[3])] return [True, math.inf, -math.inf, 0] return rec(root)[3] if someone needs python code -
@Nintendo04
@Nintendo04 2 жыл бұрын
finaly completed the entire series and got good concept of tree , thank you for quality content!
@sujan_kumar_mitra
@sujan_kumar_mitra 2 жыл бұрын
Thanks for the series, it's really helpful. Waiting for your upcoming series.
@YashveerGahlot-t6b
@YashveerGahlot-t6b 2 ай бұрын
thank u so much sir !!!! it will help me
@ayushidalal5488
@ayushidalal5488 Жыл бұрын
Finally completed this series today! Learned a lot. Thanks :)
@youracademicfriend-shashwa1051
@youracademicfriend-shashwa1051 3 ай бұрын
Thankyou Great Explanation!!
@muditkhanna8164
@muditkhanna8164 Жыл бұрын
using the similiar approach here is the code for the maximum sum BST Leetcode 1373 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ int ans; //ans declared globally class bstnode{ public: bool valid; int maxl; int minr; int sumtotal; bstnode() { valid=true; maxl=INT_MIN; minr=INT_MAX; sumtotal=0; } }; class Solution { public: bstnode calc_sum(TreeNode*root) { if(root==NULL) { return bstnode(); } bstnode curr; bstnode left=calc_sum(root->left); bstnode right=calc_sum(root->right); //why max left and min right //well the highest max in left should be less than root->val and the lowest value in right should be less than root->val in order for it to be a bst //if the bst 's right and left are valid and maxleftvalvalval; //these next two steps are generally done for the recursion part //otherwise there's not much need after getting to know that it is a bst //as we are finding from bottom to root //the right min value will be compared with the right's left and right->val for min curr.minr=min(root->val,left.minr); //the left max value will be compared with the left's right and left->val for max curr.maxl=max(root->val,right.maxl); } else { //else make it false and the sum of that non bst will be either from right or left //because together left and right with node cannot be a bst so the right and root or left and root //will make bst, that's why taking the max of them curr.valid=false; curr.sumtotal=max(left.sumtotal,right.sumtotal); } //with every recursive call ans is updated we'll wait till the base condition and keep updating answer ans=max(ans,curr.sumtotal); //the return value is the valid bst for this recursive function return curr; } int maxSumBST(TreeNode*root) { ans=0; calc_sum(root); return ans; } };
@akshaygill4692
@akshaygill4692 Жыл бұрын
I have a doubt if inorder comes as sorted ,this proofs the tree is bst right ? Now the example striver have taken also gives inorder as sorted but it is not a bst why ? Please correct me if am wrong
@aaravgulati1194
@aaravgulati1194 Жыл бұрын
See carefully the inorder traversal is not sorted
@mmmm-wm8ci
@mmmm-wm8ci 10 ай бұрын
In right subtree with root 40 how minnode is 40 instead of 30.
@parthsalat
@parthsalat 2 жыл бұрын
9:25 FIrst 19 will come then 16 will come
@sauravchandra10
@sauravchandra10 Жыл бұрын
So finally with efforts, I was able to complete this series. Commendable job striver
@sandeep-sb
@sandeep-sb Жыл бұрын
Completed the Free ka Tree Series today. Thank you, Striver. I learned a lot from this series. Onto the next one :)
@patilsoham
@patilsoham 2 жыл бұрын
did not understand: return NodeValue( min(root->val, left.minNode), max(root->val, right.maxNode), left.maxSize + right.maxSize + 1); can someone please explain?
@tejassrivastava6971
@tejassrivastava6971 2 жыл бұрын
they are used to handle the NULL case when min is INT_MAX and max is INT_MIN.
@amanbhadani8840
@amanbhadani8840 2 жыл бұрын
This is condition of a valid BST, that is --- your current root's value is greater than the maximum of all nodes in left and current root's value is lesser than the minimum of all nodes in the right. Then this is considered a valid BST and we simply in min Node we return the minimum of (roots value and the minimum of all nodes in left) and in max Node we simply return the maximum of (roots value and maximum of all nodes in right) and since its a valid case ,in max size we return the sum of all nodes in the left of root +sum of all nodes in right of root+1(root itself) to get the count of total of nodes in a valid BST. Hope, I am able to clear your doubt.
@fazilshafi8083
@fazilshafi8083 Ай бұрын
Thank you! 🔥
@jagjiwanchimkar8106
@jagjiwanchimkar8106 2 жыл бұрын
When will you start Tries series!!
@srinivasveligethi2380
@srinivasveligethi2380 Жыл бұрын
@11:27 I Guess minimal should be 30 instead of 40 becuase root should be less than or equal to minimal of right sub tree.
@priyanshkumar17
@priyanshkumar17 7 ай бұрын
Yes
@harshitparashar
@harshitparashar 2 жыл бұрын
Another possible Brute: store inorder, check largest sorted array correct or not?
@brainss5670
@brainss5670 2 жыл бұрын
I was thinking the same, also we need not to store the inorder too, we can just store previous node value in a variable and compare it with current node value, then it would be a O(n) time and O(1) space soln (not considering the recursion stack space) 🤔🤔
@harshitparashar
@harshitparashar 2 жыл бұрын
@@brainss5670 exactly That’s optimisation, basically peak n valley
@tejassrivastava6971
@tejassrivastava6971 2 жыл бұрын
Tree: 4 2 5 1 0 inorder: 1 2 0 4 5 longest sorted array: 1 2 => size = 2 but its not a subtree since it has right node = 0 as well. Therefore approach is not correct.
@brainss5670
@brainss5670 2 жыл бұрын
@@tejassrivastava6971 nice testcase man !
@abhishekchoudhary5713
@abhishekchoudhary5713 2 жыл бұрын
@@tejassrivastava6971 Thanks for saving my time..:)
@aryansinha1818
@aryansinha1818 2 жыл бұрын
Is it me only or it happened with others too, that this question took some time to be understood?
@parthsalat
@parthsalat 2 жыл бұрын
It took me also more time than expected
@aryansinha1818
@aryansinha1818 2 жыл бұрын
@@parthsalat Ok
@shreyanshmittal9381
@shreyanshmittal9381 Жыл бұрын
happened with me as well, had to watch the video twice!
@codding32world50
@codding32world50 2 жыл бұрын
Finally Compelted Yess it took me lot more time than expected... But it is with it.. Thank Youuu So much Striverrr
@kO_SE__ShivamKumar
@kO_SE__ShivamKumar Жыл бұрын
A really nice explanation again!!!
@ujjwalverma2870
@ujjwalverma2870 Жыл бұрын
Finally Completed the Free the Tree series ,Thanks Bhaiya !!
@SyedSameer-bk9pe
@SyedSameer-bk9pe Жыл бұрын
How much time it can to complete this series
@guptashashwat
@guptashashwat 2 жыл бұрын
Commenting to mark, that I have completed this playlist. Thanks Striver!
@souvickmazumdar8129
@souvickmazumdar8129 Жыл бұрын
Completed....A Big thanks to striver....Loved it
L52. Recover BST | Correct BST with two nodes swapped
15:56
take U forward
Рет қаралды 127 М.
BS-17. Aggressive Cows | Binary Search Hard
26:44
take U forward
Рет қаралды 161 М.
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
火影忍者一家
Рет қаралды 100 МЛН
Man Mocks Wife's Exercise Routine, Faces Embarrassment at Work #shorts
00:32
Fabiosa Best Lifehacks
Рет қаралды 6 МЛН
Как подписать? 😂 #shorts
00:10
Денис Кукояка
Рет қаралды 8 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 408 М.
L28. Maximum Width of Binary Tree | C++ | Java
22:41
take U forward
Рет қаралды 262 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 403 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 215 М.
BS-18. Allocate Books or Book Allocation | Hard Binary Search
27:29
take U forward
Рет қаралды 169 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 658 М.
L8. Trapping Rainwater | 2 Approaches | Stack and Queue Playlist
28:58