What a co incidence, I was exactly studying the same problem and wasn't able to understand on my own and here comes Striver for rescue 😀😀
@ajaykr281110 ай бұрын
instead of making third variable we can also update the middle variable when there is a 2nd violation
@stith_pragya7 ай бұрын
Thank You So Much for this wonderful video...🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@chitranshjain50752 жыл бұрын
Such a brilliant explanation of the problem! Thank you very much fir helping all of us!
@dhrutidesai2783 ай бұрын
Thankyou sir👍For always helping by your approaches
@namratam1522 Жыл бұрын
You are the best instructor !! Thanks a ton for this content ! You are bringing a revolution striver!
@jiteshsingh4899 Жыл бұрын
my mom said "ye ladka kitni mehnat karta h" - what a explanation striver bhaiya
@shinewanna3959 Жыл бұрын
As u initialize prev as INT_MIN then u don't need to check prev != null in inorder recursive. Just correction. You are already great.
@darkexodus640410 ай бұрын
In leetcode question the values are in range of [INT_MIN, INT_MAX], so this won't work there.
@Ramu91196 ай бұрын
excellent explanation striver bhai
@ZebraZodiacАй бұрын
If trying to code in python use list to store prev,first,last and middle (or prev and last if not using middle) as when prev is just declared as just an argument, it gets reassigned and will not get passed on to the next recursive call.
@preetisahani50549 ай бұрын
Awesome explanation
@tempbot7190Ай бұрын
Much Love from London❤
@krishnaradhey281411 ай бұрын
LeetCode 99 problem Can be done with morris traversal
@adebisisheriff1595 ай бұрын
Thanks Man!!!
@shashankbajpai56592 жыл бұрын
add morris traversal and we can do this problem on O(1) space as well.
@jerrywong51402 жыл бұрын
Very helpful and thorough explanation, love it!
@ujjwalverma5893 Жыл бұрын
We can avoid the middle pointer also just keep updating the first and second pointers - class Solution { private TreeNode first; private TreeNode second; private TreeNode prev; public void recoverTree(TreeNode root) { inorder(root); int temp = first.val; first.val = second.val; second.val = temp; } public void inorder(TreeNode root) { if(root == null) return; inorder(root.left); // Keep updating first and second which points to first and second numbers to swap if(prev != null && root.val < prev.val) { if(first == null) { first = prev; second = root; } else { second = root; } } prev = root; inorder(root.right); } }
@PRANAVMAPPOLI Жыл бұрын
Thought same ✌️💯
@amanbhadani88402 жыл бұрын
Absolutely brilliant explanation as well as mind blowing implementation of code,just loved it bhaiya.
@user-tk2vg5jt3l3 ай бұрын
Thank you Bhaiya
@SharuxD Жыл бұрын
The logic you have given to solve this is brilliant af. GG
@timjoyalle3182 жыл бұрын
This dude's neighbors get free lectures. Hope they appreciate it.
@kalpeshmali14762 жыл бұрын
After watching your tree series i m pretty confident in Binary trees and bst 🔥🔥🔥🔥🔥🔥🔥
@studyafa71594 ай бұрын
i think there is no need of " prev != null" in line 27
@pratikmhatre4815 Жыл бұрын
I always feel motivated by your passion of explaining problems👍
@SatyamKumar-bw4vi Жыл бұрын
Hare Krishna..! Got Placed in R & D of a Product Based Company..! Thanks Bhaiya..! I will tag you in Linkedln post in some time
@ishakhutafale116310 күн бұрын
Watch these videos, and you'll never forget the importance of inorder traversal for BSTs!
find the two elements which are not at it's correct place through inorder traversal vector and sorted vector. again perform inorder traversal and have a check on root element if it's equal to incorrect element swap it.
@harshitjaiswal94394 ай бұрын
understood.
@abhinanda70492 ай бұрын
understood
@shagunlamba64812 жыл бұрын
Great explanation , thank you.
@anubhavpabby68562 жыл бұрын
Thank you striver bhaiya for making such a great series and it's helping me out in placement preparation
@parthsalat Жыл бұрын
You seem to like teddy bears a lot
@gandhijainamgunvantkumar67832 жыл бұрын
Thank you so much striver bhaiya for providing such an amazing content :)
We have to make variable i, a global variable. So, that it can get updated after every recursive call.
@stith_pragya7 ай бұрын
🚨[IMPROVMENT IN THE CODE]:🚨 Hello Striver Bhaiya, 1- if you would have done middle = root in the else part then you wouldnt have had any requirement of the variable "last", you can do it using only two variables. 2- You could have also used an array of size 3 instead of global variables. But if your intention to was make code simpler for others to understand then it is all fine.....👍
@gyanendrasaurav8775 ай бұрын
i also solved it using two vaiables because if in first case we can find the prev value at which the root value is less than prev then that means we find the greater element so the next voilation is always less than the first voilation so we can store last as root
@shantanukumar40812 жыл бұрын
Great Explanation !!!
@prashantsahu62122 жыл бұрын
Just wow, the intution was awesome.
@sparshsharma60682 жыл бұрын
Amazing explanation bhaiya!
@vinaygoswami53742 жыл бұрын
Quite ambiguous explanation.
@tusharagarwal53063 ай бұрын
13:55 which online coding judge or editor is that??
@sreekanthyadav5801Ай бұрын
For many problems in BST, MORRIS Inorder approach is giving Stack Overflow error (RUN TIME ERROR). Is it same with everybody ?
@prasadm3614 Жыл бұрын
Congratulations on 3 Lakh Subscribers
@shivambajpeyi90082 жыл бұрын
this was smooth!
@keshavagrawal1027Ай бұрын
can someone tell me why in the last we do (prev=root ) pls if u know try to give a explanation...🙏
@lavanyaprakashjampana933 Жыл бұрын
we love your content and we love you......🤟🖤
@muditkhanna816410 ай бұрын
but what if there are more than one nodes that are swapped?
@papayasprout2 жыл бұрын
Thanks for the video man.
@peddikarthik7832 Жыл бұрын
even if we keep all variables and functions of class in public, code still works. But why are we keeping private for some functions and variables ?
@adiin-1940 Жыл бұрын
Shouldn't line number 24 of C++ be , if(prev ->Val != INT_MIN &&.....) rather than if(prev!=NULL &&....) because prev has already been set to a TreeNode with a value of INT_MIN so it will never be NULL?
@mayankbharti531 Жыл бұрын
just using "if(root->val < prev->val)" is fine as the first root->val will always be greater than the INT_MIN so it automatically won't check for the first node.
@niranjansaha5135 Жыл бұрын
the first method can be optimised to O(n) + O(n) time, by removing the redundant sorting, void dfs(Node* root, vector &inorder){ if(root == NULL) return; dfs(root->left, inorder); inorder.push_back(root); dfs(root->right, inorder); } void correctBST( struct Node* root ) { vector inorder; dfs(root, inorder); int ct = 0; vector pos; for(int i = 1; i < inorder.size(); i++){ if(inorder[i]->data < inorder[i-1]->data){ pos.push_back(i); ct++; } } if(ct == 1){ swap(inorder[pos[0] - 1]->data, inorder[pos[0]]->data); } else if(ct == 2){ swap(inorder[pos[0]-1]->data, inorder[pos[1]]->data); } }
@JujareVinayak Жыл бұрын
Which device is used in videos?? I need one to practice.
@silicon9794 Жыл бұрын
Perfectly explained....
@siddhaarthsingh74142 жыл бұрын
Why prev = new Tree(INT_MIN) That is not required !!!! Pls Anyone ???
@arpitjaswal4210 Жыл бұрын
I simply made the prev node NULL and the code still got accepted.
yup, class Solution { TreeNode *prev; TreeNode *first; // TreeNode *middle; TreeNode *last; public: void inorder(TreeNode* root){ if(!root)return; inorder(root->left); // the node is not the root element if(prev != NULL and (root->val < prev->val)){ // if this is the first element if(first == NULL){ first = prev; } last = root; } prev = root; inorder(root->right); } void recoverTree(TreeNode* root) { prev = first = last = NULL; inorder(root); swap(first->val,last->val); } };
@ErfanHossainShoaib Жыл бұрын
If the inorder sequence is 3 25 7 8 10 15 20 12. Then...
We dont need to check the condition if prev!=NULL ;
@user-gq3xu4vj8d Жыл бұрын
bhaiya you are great
@justinmyth49802 жыл бұрын
Why you have used middle,we can just update last instead of middle and it works fine??
@placementbaadshah86042 жыл бұрын
kzbin.info/www/bejne/oWPLkoCqhZyhrNU
@your_name962 жыл бұрын
I guess it works for understanding the algorithm then optimising it to first and last become easier class Solution { TreeNode *prev; TreeNode *first; // TreeNode *middle; TreeNode *last; public: void inorder(TreeNode* root){ if(!root)return; inorder(root->left); // the node is not the root element if(prev != NULL and (root->val < prev->val)){ // if this is the first element if(first == NULL){ first = prev; } last = root; } prev = root; inorder(root->right); } void recoverTree(TreeNode* root) { prev = first = last = NULL; inorder(root); swap(first->val,last->val); } };
@justinmyth49802 жыл бұрын
@@your_name96 thanks bro btw which are u a college student?
@mohit9975 Жыл бұрын
@@justinmyth4980 Islamabad university , lahore
@dreamyme543 Жыл бұрын
Understood:)
@girikgarg8 Жыл бұрын
Done!!
@adarshkumargupta. Жыл бұрын
bhai code bahut tej explain karte ho thoda slow karo yaar
@vibhu6132 жыл бұрын
Hey interviewer😆😅
@gauravshukla520311 ай бұрын
If bst is not in correct order you will not get the preorder sorted
@ankurshukla64622 жыл бұрын
Why you need to allocate space to prev? I don’t think we need it.
@placementbaadshah86042 жыл бұрын
kzbin.info/www/bejne/oWPLkoCqhZyhrNU
@girikgarg12682 жыл бұрын
Is it not possible that there are more than two violations for example three or four violations? Why have we considered that either there will be one violation or two violations?
@RaunakKumar-yr3zv2 жыл бұрын
Because the question states that only 2 nodes will be swapped
Bhai I think prev is never going to be NULL, because at first we are assigning it with INT_MIN and after that it always stores non-null root value. Wo condition hata ke dekho code chalega
@your_name962 жыл бұрын
@@Xp-Sam ha prev ko NULL kar de instead of INT_MIN tabh bhi chalega, my code without middle pointer, easy to optimize if yo examine the conditions in main function: class Solution { TreeNode *prev; TreeNode *first; // TreeNode *middle; TreeNode *last; public: void inorder(TreeNode* root){ if(!root)return; inorder(root->left); // the node is not the root element if(prev != NULL and (root->val < prev->val)){ // if this is the first element if(first == NULL){ first = prev; } last = root; } prev = root; inorder(root->right); } void recoverTree(TreeNode* root) { prev = first = middle = last = NULL; inorder(root); swap(first->val,last->val); } };
@rks3522 Жыл бұрын
13:10
@harshdasila668010 ай бұрын
Goat 🐐
@rohanmadiratta64215 ай бұрын
shouldnt first be equal to root..how come first is set equal to prev
@amitswami31392 жыл бұрын
Can I do it using the concept which you are using in the last lecture (largest bst) when the condition get wrong largest of left
@placementbaadshah86042 жыл бұрын
kzbin.info/www/bejne/oWPLkoCqhZyhrNU
@yatri6329 Жыл бұрын
Baak bakwas krta h Khali kuch sahi se explain nhi krta h
@satyamchauhan97752 жыл бұрын
I have done it in n time and n space using hashmap , and i was thinking my solution is best until i watched this video🥲