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 🙏🏻
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 😁..
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
@@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
9:37 ,at root =14 , he wrote [size,max,min] and at root=18 ,he wrote [size,min,max] so dont confuse he suffle the order take order [size,max,min] max come form right size and min come for left size for creating a [size,max,min]
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
**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.
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 ✌❤
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❤️❤️
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.
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.
@@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.
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..
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 !❤❤
finally today 06/11/2024 i have completed this playlist .. during this period i had my exams too , i got sick for 1 week so i didn't study for 1 week thank you striver for this amazing series
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 !!
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 : ).
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.
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
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...❣️💫🙌😊🙏
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 🙌🤩
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
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!!
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?
@@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
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.
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. :)
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; } };
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?
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.
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; } };
Easy to understand working code for python developers def largestBst(self, root): # Your code here def solve(node): if not node: return [float("inf"), float("-inf"),0] left = solve(node.left) right = solve(node.right) if left[1]
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 🙏🏻
Thanks for creating this series.
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 😁..
completely Lovedd this seriess!! thanks a lotttt
Thanks bro , It was absolutely great...
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
CONGRATS to everyone else who successfully completed this playlist!
Did you get a job after learning DSA?
@@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
@@ultimategyani6412 congratulations
@@kushagraahire1871 thanks brother
@@ultimategyani6412 what were your projects?
Completed the entire FREE KA TREE series! Learnt a lot! Thank you Striver for putting up such quality content!
11:27 Minimal will be 30 and not 40 !! Thank you striver for this series!! Helped me alot !!
@@namashsharma7550tum toh lodu ho fir..😂
Correction : At 11:30 , the minimum for subtree with root=40 is 30 not 40 . Please provide an edit at this juncture
It made my head spin
true;
True
Bhai yrr mera dimag kharab ho gya .......
@@Now_I_am_all_fired_up L lag gaye the mere 😓
searching for this comment
Completed the Tree series in 10 days.
Thank you STRIVER for making such a quality content .
9:37 ,at root =14 , he wrote [size,max,min] and at root=18 ,he wrote [size,min,max] so dont confuse he suffle the order take order [size,max,min] max come form right size and min come for left size for creating a [size,max,min]
thanks man!!
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
**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.
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 ✌❤
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❤️❤️
The hardest tree question I came across but thanks to you, now it's completely clear!!
completed whole playlist today, thank striver
completed the entire tree series. Enjoyed a lot! thank you for all the content striver!
Mistake at 11:28 , For 40, smallest will be 30 not 40
S
Completed the entire series today. Feeling much more confident now. Thank you very much Stiver! 😃
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.
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.
currently watching the last video it took me 14 days.
like how many videos per day
Did you save/write the codes somewhere, like in a notebook or notion? Because storing codes takes up a lot of my time.
Yes @parth I have noted them down in a notebook...not the full code but just the pseudo code
@@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.
A great series to learn tree from scratch. Thanks for top notch content. It was really very helpful.
Just completed your amazing tree series Striver and thanks for making such a wonderful series for free :)
Earlier trees and graphs haunt me. Completed this playlist and now questions are solvable. Thank you striver.
bhai are these playlist to understand concept easily ?? and is whole DSA complete here ?
@@closetoheart6120 tree playlist are awesome u can follow this according to me
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..
It is such a Great Series. More power to Striver. You are the superhero of students like us.
This is one of the best videos of the playlist !!! Thanks Striver bhaiya :)) You made trees easier to understand & visualize...
Thanks Striver for this incredible Tree Series , have learnt a lot from this series , and now I am gonna checkout Graph series.
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 !❤❤
finally today 06/11/2024 i have completed this playlist .. during this period i had my exams too , i got sick for 1 week so i didn't study for 1 week thank you striver for this amazing series
**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)};
}
another playlist completed.. thanks for bringing a change in other's lives
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 !!
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 : ).
Just completed the Tree series playlist....absolutely mindblowing content with highest level of perfection !!!!
Thanks sir , Because of you another topic of dsa is completed.
when null occur do min=MAX INTEGER and max=MIN INTEGER |||||||||||||| Range is not follow the rule the max=MAX INTEGER AND min=MIN INTEGER 😉
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.
yes, it should work.
length of longest increasing sub array
no
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
i Came up this approach first when I seen the question
Completed this amazing free ka tree series today
solution with cam makes more easier to understand the concept. Thanks!
Thanks tou striver for whole series👐✨
495k to 137k itself speaks many things
well done striver bhaiya
630k*
Finally i successfully completed the free ka tree series thanks striver bhaiya🧡
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❤
This series is also completed ❤🎉 done and dusted
Finally completed this series. Thanks a lot Striver. You have helped a lot. This truly was the best tutorial on trees. 🔥🔥🔥
Thanks!
@takeUforward @striver Loved the entire tree series!
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...❣️💫🙌😊🙏
Finally completed
Thanks striver bhaiya
You owe my respect 📈❤️
Completed successfully!! Now revision is the only thing to do !! and will do it!!
Thanks for the series...I'd have never learnt BST if it wasn't for this series
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 🙌🤩
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
finallyy completed the whole series....thanks a lot striver for such a wonderful series on tree...💫
I CAN CONFIDENTLY SAY THAT THIS IS THE ABSOLUTE BEST SERIES ON TH-cam FIR TREES; LAST QUESTIONS WERE JUST TOP NOTCH . LOVED IT THANKS STRIVER .
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!!
Loved the series ❤ thank you
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?
This approch fails i also did this one
@@sarthakragwan5248 why, can you give some insight
@@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
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.
code is not working bro
bhai kitna pyaara soln likha hai 😍
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.!
At size 4 the minimal should be min(40,30) = 30 not 40
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.
Same doubt...did you get any explanation?...surely has to be O(N)
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
Very good concept
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. :)
completed the list.. thank you so much..
Finally completed the entire Free Ka Tree Series....Thank you so much striver bhaiya for putting in all the efforts...You are the best.
9:25 FIrst 19 will come then 16 will come
Completed the whole tree series today.... Learnt alot thank you Striver the saviour 🥰🥰
Completed the tree series.Thanks for making this amazing tree series.
Completed the series in 2 weeks ,thank you Striver for such wonderful playlist.
I took 3-4 weeks 🙂
Completed the Free ka Tree Series today. Thank you, Striver. I learned a lot from this series. Onto the next one :)
amazing explanation, thanks!
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;
}
};
Thank you! 🔥
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?
Merge two BSTs is missing in this series, please upload that too! Thanks in advance :)
Thanks man!!
this series really helped me to solve tree problems which I found quite difficult while l solving tree questions.
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.
Completed the Series today, developed logic, and made notes, next is the Graph series. Journey never ends✌✌
@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.
Yes
thank u so much sir !!!!
it will help me
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;
}
};
In the first example, why isn't 10 included in the BST? (10,5,1,8)
Could you please give notes of the tree series it is incomplete as the notes and the effective explanation makes the series effective.
Thankyou
Great Explanation!!
finaly completed the entire series and got good concept of tree , thank you for quality content!
i like brute force also, putting memorization for sub branches.
UNDERSTOOD;
How many more episodes left in the tree series?
A really nice explanation again!!!
// simple implementation using vector
class Solution{
int sum =0;
public:
vector maxSum(Node *root) {
if(root == NULL)
return {INT_MAX, INT_MIN, 0};
vector l = maxSum(root->left);
vector r = maxSum(root->right);
if(l.size() == 0 || r.size() == 0|| root->data data >= r[0])
return {} ;
vector ans(3);
ans[0] = min(root->data, l[0]);
ans[1] = max(root->data, r[1]);
ans[2] = root->data + l[2] + r[2];
sum = max(sum, ans[2]);
return ans;
}
int largestBst(Node *root)
{
maxSum(root);
return sum;
}
};
it got more space complexity/
ky ye series yaha samaapt hoti hai? Thank you for this free playlist ❤
Completed this playlist sucessfully
Easy to understand working code for python developers
def largestBst(self, root):
# Your code here
def solve(node):
if not node:
return [float("inf"), float("-inf"),0]
left = solve(node.left)
right = solve(node.right)
if left[1]
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 -
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
mm mmm problem problem --striver...Loved it man! 9:50
In right subtree with root 40 how minnode is 40 instead of 30.
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
Finally Compelted Yess it took me lot more time than expected... But it is with it.. Thank Youuu So much Striverrr
Thanks for the series, it's really helpful.
Waiting for your upcoming series.