It is easy to go through, and everything is so nice, besides if the binary tree contains Integer.MAX_VALUE layer, for example, the call stack may be passing away.
@kylanali95753 жыл бұрын
you probably dont give a damn but does any of you know of a way to log back into an Instagram account?? I was stupid lost the login password. I would love any tips you can give me
@coltonjad48253 жыл бұрын
@Kylan Ali instablaster =)
@ok123ut4 жыл бұрын
the way this was explained in 11 min. I don't think i can find more valuable solutions for this in such a short time anywhere else. Amazing!
@depression_plusplus61203 жыл бұрын
After 5 years he's still helping
@aravindreddy17923 жыл бұрын
Yep
@prelimsiscoming3 жыл бұрын
tera naam mast hai
@depression_plusplus61203 жыл бұрын
@@prelimsiscoming thanks..
@xuwang32535 жыл бұрын
I love this guy's multiple walking-through examples on white board!
@kevinbaijnath69747 жыл бұрын
Wow, great explanation about the algorithm! I appreciated the fact that you gave multiple examples that tested different things!
@guru007pavan4 жыл бұрын
Too good explanation. short and sweet. I can say this particular solution is way better explained than Back ToBack SWE. Thanks and appreciate your effort...
@hrishikeshkonderu16384 жыл бұрын
Yes u r correct
@sharansingh49563 жыл бұрын
After i get tired of the logics and code explained by everyone, i do watch your video to understand it in layman and practical way.
@shreekantwaphare60638 жыл бұрын
The space complexity of both the algorithms is O(height), because the recursive algorithm will have function calls on the stack.
@mtjokro3 жыл бұрын
Thank you! Finally a video that helped me understand the recursive details of the various cases of binary tree
@The_Promised_Neverland...3 жыл бұрын
Can't agree more
@cosmicgirl9104 жыл бұрын
Thank you for covering multiple cases. Other videos didn’t explain as thoroughly as you and didn’t go through many test cases
@dianel.3448 жыл бұрын
Thank you, sir! I was confused about this recursive code, but now I understand how it works perfectly!
@dnl_blkv7 жыл бұрын
Another great explanation with a beautiful solution!
@abdallaelmedani89333 жыл бұрын
This is a very useful, well explained, and simple algorithm. Thank you, Tushar.
@jlecampana4 жыл бұрын
Best Video on LCA available on KZbin, Thanks Tushar!
@leonlew13868 жыл бұрын
This was a great visual walkthrough. Clear and concise.
@learnsharegrow72942 жыл бұрын
Nicely explained. This is a popular problem statement in Amazon interviews.
@AlancRodriguez2 жыл бұрын
Six years later and im still learning from you
@robinsoni47784 жыл бұрын
The other solution also takes the implicit stack during recursion, so yes it does require the extra space.
@cachegrk8 жыл бұрын
You are videos are just awesome! Great job! Can't imagine prep without your videos!
@generalroboskel7 жыл бұрын
Excellent video/explanation. Your videos make algorithms so much easier.
@jinny50253 жыл бұрын
Your explanation always hits the spot. Thanks :)
@decisionguider49114 жыл бұрын
Great job Tushar! Absolutely awesome walkthrough..
@lpatrasco5 жыл бұрын
Clear and straight to the point.
@b9944236 Жыл бұрын
Last example is really important to explain both nodes in the same path.
@foreverursabhi3 жыл бұрын
Optimization note, if the left subtree search yielded the LCA, there's no need to search the right subtree. This can only be avoided by keeping track of how many of the target nodes have been found; it's not sufficient to check if the left subtree search returned a node that is not one of the target nodes, because one of the target nodes may as well be the LCA. For example, 6, and 2.
@dileepbc59012 жыл бұрын
ASSUMTION: We may assume that either both n1 and n2 are present in the tree or none of them are present.
@dileepbc59012 жыл бұрын
NOTE : optimisation not works for special case
@sase10173 жыл бұрын
Excellence! Real knowledge, thank you.
@joycer52933 жыл бұрын
thank you so much this is the most helpful video ive found for this question
@sleepypanda71723 жыл бұрын
great explanation! this dry run really helped me a lot
@shubhamkale7353 жыл бұрын
Thank you sir for this dry run feeling lucky to find this video ... Keep making such dry run video sir Thank you
@nimishbajaj38158 жыл бұрын
Great work, as you recommended I started solving problems on GeeksForGeeks, your videos + that website together make up a great learning experience thanks again, keep up the good work !
@biboswanroy66994 жыл бұрын
In the recursive solution isn't anyway space is consumed due to stacks
@MIRAZIB4 жыл бұрын
yes, obviously .. space complexity is O( h ) .. worst case will occur when the tree is skewed and the ancestor lies beneath of it.
@harshitpruthi57444 жыл бұрын
I think the method you explained above is based on the assumption that both n1 and n2 are present Can you please explain what if one of the two or both nodes aren't present in Binary tree Thanks in Advance
@SuperHARIS142 жыл бұрын
its given in the question that both n1 and n2 will always be there in the tree.
@antuancaraballo96918 жыл бұрын
Love your explanations Tushar! You are awesome!
@14rucha8 жыл бұрын
Awesome video! Your explanation is too good. I can understand recursion very easily through your video.
@mayankmittal65524 жыл бұрын
man, your hair style and explanation a true entertainer!!
@uRamPlus4 жыл бұрын
Hahahaha !!! He’s cool tho
@nadiiaparsons60882 жыл бұрын
It's much clear now. Thanks a lot for your explanation.
@vuleenguyen5 жыл бұрын
Thanks a lot. You gave me the idea to solve this problem
@sase10173 жыл бұрын
Thanks for the great explanation
@sumedhaj90172 жыл бұрын
Very nice explanation with the examples!
@sureshgarine2 жыл бұрын
hi Thushar, so nice of you...explained very well
@levtunik9974 жыл бұрын
great explanation, you have high-quality videos..thanks.
@AP-eh6gr8 жыл бұрын
in the last 8,7 example, is there an assumption that both given nodes whose ancestor we are looking for, have to be present in the tree? bcoz we returned 8, and never went down to 7 to see if it was actually there or not
@hemanthp23677 жыл бұрын
I think we should make assumptions clear that is both nodes should exist and nodes are not equal, tree with one node etc? static Node lca(Node rootNode, int a, int b, AtomicBoolean found) { if (rootNode == null) { return null; } if (found.get()) { return null; } Node left = lca(rootNode.left, a, b, found); Node right = lca(rootNode.right, a, b, found); if (left != null && right != null) { found.set(true); return rootNode; } final boolean rootMatched = rootNode.i == a || rootNode.i == b; if (rootMatched) { if (left != null || right != null || a == b) { found.set(rootMatched); } return rootNode; } if (left != null) { return left; } else if (right != null) { return right; } else { return null; } } This should work for all the cases. I used AtomicBoolean but can be replaced by MutableBoolean. You have a match only when found returns true.
@mrdude10845 жыл бұрын
I saw the solution to this problem over gfg but couldn't get the part of searching even after we were using maps for given 2 values... thnx for your question.. helped me alot
@nishantharava91815 жыл бұрын
Even if we traverse until node 7, it would eventually return the same value 8 to the root of the tree. This case holds true every time when one of the nodes is a ancestor.
@abhinavmishra76174 жыл бұрын
this algo is based ln the assumption that that both nodes will be present in the tree...nice observation btw
@abhinavmishra76174 жыл бұрын
4 years ago....damn
@HieuLe-mh4bc2 жыл бұрын
Great explanation! Thank you for the video.
@anandsakthivel94257 жыл бұрын
Thank you so much for your contribution..I think it is one of the best lectures i have ever seen for like this focusing on interview problems alone..Again thank you so much...please keep on share more and more challenging problems
@kfliden3 жыл бұрын
Great explanation as usual!
@shreyaa_214 жыл бұрын
Recursion Explained nicely, thanks
@stonecoldcold29414 жыл бұрын
Awsome Explanation Bro!!
@444not3 жыл бұрын
Thank you for the explanation. Very helpful.
@trinityartworld97323 жыл бұрын
what if, if n1=8 and n2=17. n2 doesn't exist as per this code it will return 8 but this is not correct. Can you please cover this scenario as well?
@rohitkumar-yz5qy6 жыл бұрын
you have given best explanation..thanks a lot
@shubham7095 жыл бұрын
Great job, the explanation is very clear and lucid. Thanks a lot :).
@shubhamtalks97184 жыл бұрын
Amazing explanation!!!
@gourab4692 жыл бұрын
Excellent explanation! Hats off!
@nikkis81027 жыл бұрын
Thank you so much, Tushar!!
@RoshanKumar-nx6rk3 жыл бұрын
Your second algorithm has also O(n) space complexity!!! You can't neglect the function stack calls.
@reassume48262 жыл бұрын
above & beyond awesomeness!
@hmoiz526 жыл бұрын
Very nicely explained! Thanks.
@chrisy.7032 жыл бұрын
best lca video in youtube so far!
@GowthamBharadwaj3 жыл бұрын
Very lucid explanation
@learnsharegrow79503 жыл бұрын
Very well explained. Small correction required in your code, instead of doing nodes equals comparison using '==', you need to do node data comparison as shown below. if(root.data == n1.data || root.data == n2.data){ return root; }
@RightMeow283 жыл бұрын
That doesn't need to be corrected. The value is irrelevant. Node n1 and n2 hold the address of the nodes that need to be found. All you need to do is compare the address.
@Sameer885305 жыл бұрын
Checking only one node should not be enough . What if the 8 is present on the left and right of the 11 then 11 will be lowest common ancestor . @Tushar checked 11 and then directly returned 11 which is wrong.
@shreyasingh-sn4bs7 жыл бұрын
I have become a fan! Great work Sir :)
@rhondawang26393 жыл бұрын
Thank you so much!
@aloksharma34806 жыл бұрын
In this algorith the 2nd last line "if(left==NULL && right==NULL) return NULL;" is not required as last line will return NULL if both are NULL.
@samratdr4 жыл бұрын
Thank you for the video. One feedback I have is that the mic is too hard for the ears
@evantheking7 жыл бұрын
finally good explanation. I understand it now
@whyarewehere95555 жыл бұрын
Thank you sir
@bumbarumbado8 жыл бұрын
beautiful explanation!
@nitinagrawal66374 жыл бұрын
I checked various sites, even during interviews of big companies & everywhere the same kind of solution is given & I have following issues with this problem & I wonder that why people don't raise this concern, either they don't understand English or they are just interested in clearing the interviews to get the money - a) How can you assume that both nodes are present in the tree without saying it anywhere. b) Because above assumption is missing then you might be returning wrong answer even if there is not common ancestor, rather we should say that one or both the given nodes not found. c) When you use some other language's word in your problems then respect the meaning of that word also or don't use that word at all. How can I be my own ancestor in any condition, so how can 8 be its own ancestor. So if you have different definition then use different word also, say like find the common meeting point etc. But using the word 'Ancestor' here is not right. Improvement - If we follow the correct definition of Ancestor then we can apply the same solution to find the common ancestor for multiple nodes. Assumption : All nodes in the tree are unique.
@mehrdadk.68164 жыл бұрын
complexity can be O(logn) since it is a binary tree
@xxbighotshotxx7 жыл бұрын
Thank you for the explanation! I was able to write the code for it myself as a result!
@bipul21384 жыл бұрын
Genius approach...well explained
@atulavhad16617 жыл бұрын
@ 6:31 the idea to return 11 from the node itself is wrong because the other node 8 could have been a child of 11. So even if we find 11 we need to explore the tree below and ensure the absence of the other node 8.
@prathashukla65965 жыл бұрын
@tushar can you cover this edge case in your algo.
@AKHILESHKUMAR-nk2rk4 жыл бұрын
it is assumed that the two nodes are present in the tree
@ShaliniNegi244 жыл бұрын
Very nice Explanation.
@carloslaguna70368 жыл бұрын
Correct me if I'm wrong: In your last example. The algorithm does not take into account the possibility that 7 is not part of the tree.
@haoli29762 жыл бұрын
Genius. Thank you very much.
@adampatrick76874 жыл бұрын
Really really helpful.
@ayushpalak8 жыл бұрын
great video Tushar. Thanks for taking out your time to explain all these stuffs. They really help a lot. Btw, do you speak Hindi ? :P
@mohammedghabyen7213 жыл бұрын
Great explanation, Thanks a lot
@GaneshSatputeAtPlus4 жыл бұрын
Here we assume that both nodes are present. If one of them is not present, we return a wrong output.
@madiyar80795 жыл бұрын
amazing!! Thanks Tushar! PLease continue!
@chetansarnad12324 жыл бұрын
Thanks for the video, really helpful
@BhatiJhabarSingh3 жыл бұрын
Excellent Explaination
@jubiliudu2 жыл бұрын
Your solutions are amazing!! Keep doing it :)
@MLAICS5 жыл бұрын
The recursive approach uses far more space than finding the paths to the nodes. In terms of space efficiency the approach taken in the first two minutes is actually more efficient!
@0xeb-3 жыл бұрын
Perfect. Thank you.
@HoanNguyen-fc8vb2 жыл бұрын
Very nice. Thank you very much.
@volodymyrkot62775 жыл бұрын
Not sure if the described solution is optimal. It still takes O(N) space because of the recursion, and that is stack space, which is much more precious.
@RottenWoodInPower4 жыл бұрын
Very clear. Thanks
@jaspertienny89768 жыл бұрын
The amazing Tushar. Thanks!
@rajusaratkar25998 жыл бұрын
Very nice and Clear Explanation ..Thanks for the Video.
@shivampandey88373 жыл бұрын
No bro
@shimul66802 жыл бұрын
Thank you very much.....! Super solution!!!
@harshkanani66052 жыл бұрын
Very nice explanation thank you sir
@jacobstech17773 жыл бұрын
i really love this guy
@umberto32712 жыл бұрын
this is excellent, thank you
@SiddharthKulkarniN7 жыл бұрын
Great videos dude. Thanks!
@Balaji-cv5bz4 жыл бұрын
Hey Tushar, wonderful explanation. can you please post a video of an inorder successor of a binary search tree. Thanks!
@adithiaa43854 жыл бұрын
Great explanation! Thank you.!
@manishbhatt36148 жыл бұрын
@Tushar: Good work and good explanations.
@liangchen44394 жыл бұрын
A follow up of this question is to find the LCA of a list of nodes (more then 2) in this tree. Any ideas?