Lowest Common Ancestor Binary Tree

  Рет қаралды 251,902

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

Пікірлер: 334
@cheofusi3562
@cheofusi3562 4 жыл бұрын
Recursion is indeed a divine art
@blatogh1277
@blatogh1277 3 жыл бұрын
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.
@kylanali9575
@kylanali9575 3 жыл бұрын
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
@coltonjad4825
@coltonjad4825 3 жыл бұрын
@Kylan Ali instablaster =)
@ok123ut
@ok123ut 4 жыл бұрын
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_plusplus6120
@depression_plusplus6120 3 жыл бұрын
After 5 years he's still helping
@aravindreddy1792
@aravindreddy1792 3 жыл бұрын
Yep
@prelimsiscoming
@prelimsiscoming 3 жыл бұрын
tera naam mast hai
@depression_plusplus6120
@depression_plusplus6120 3 жыл бұрын
@@prelimsiscoming thanks..
@xuwang3253
@xuwang3253 5 жыл бұрын
I love this guy's multiple walking-through examples on white board!
@kevinbaijnath6974
@kevinbaijnath6974 7 жыл бұрын
Wow, great explanation about the algorithm! I appreciated the fact that you gave multiple examples that tested different things!
@guru007pavan
@guru007pavan 4 жыл бұрын
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...
@hrishikeshkonderu1638
@hrishikeshkonderu1638 4 жыл бұрын
Yes u r correct
@sharansingh4956
@sharansingh4956 3 жыл бұрын
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.
@shreekantwaphare6063
@shreekantwaphare6063 8 жыл бұрын
The space complexity of both the algorithms is O(height), because the recursive algorithm will have function calls on the stack.
@mtjokro
@mtjokro 3 жыл бұрын
Thank you! Finally a video that helped me understand the recursive details of the various cases of binary tree
@The_Promised_Neverland...
@The_Promised_Neverland... 3 жыл бұрын
Can't agree more
@cosmicgirl910
@cosmicgirl910 4 жыл бұрын
Thank you for covering multiple cases. Other videos didn’t explain as thoroughly as you and didn’t go through many test cases
@dianel.344
@dianel.344 8 жыл бұрын
Thank you, sir! I was confused about this recursive code, but now I understand how it works perfectly!
@dnl_blkv
@dnl_blkv 7 жыл бұрын
Another great explanation with a beautiful solution!
@abdallaelmedani8933
@abdallaelmedani8933 3 жыл бұрын
This is a very useful, well explained, and simple algorithm. Thank you, Tushar.
@jlecampana
@jlecampana 4 жыл бұрын
Best Video on LCA available on KZbin, Thanks Tushar!
@leonlew1386
@leonlew1386 8 жыл бұрын
This was a great visual walkthrough. Clear and concise.
@learnsharegrow7294
@learnsharegrow7294 2 жыл бұрын
Nicely explained. This is a popular problem statement in Amazon interviews.
@AlancRodriguez
@AlancRodriguez 2 жыл бұрын
Six years later and im still learning from you
@robinsoni4778
@robinsoni4778 4 жыл бұрын
The other solution also takes the implicit stack during recursion, so yes it does require the extra space.
@cachegrk
@cachegrk 8 жыл бұрын
You are videos are just awesome! Great job! Can't imagine prep without your videos!
@generalroboskel
@generalroboskel 7 жыл бұрын
Excellent video/explanation. Your videos make algorithms so much easier.
@jinny5025
@jinny5025 3 жыл бұрын
Your explanation always hits the spot. Thanks :)
@decisionguider4911
@decisionguider4911 4 жыл бұрын
Great job Tushar! Absolutely awesome walkthrough..
@lpatrasco
@lpatrasco 5 жыл бұрын
Clear and straight to the point.
@b9944236
@b9944236 Жыл бұрын
Last example is really important to explain both nodes in the same path.
@foreverursabhi
@foreverursabhi 3 жыл бұрын
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.
@dileepbc5901
@dileepbc5901 2 жыл бұрын
ASSUMTION: We may assume that either both n1 and n2 are present in the tree or none of them are present.
@dileepbc5901
@dileepbc5901 2 жыл бұрын
NOTE : optimisation not works for special case
@sase1017
@sase1017 3 жыл бұрын
Excellence! Real knowledge, thank you.
@joycer5293
@joycer5293 3 жыл бұрын
thank you so much this is the most helpful video ive found for this question
@sleepypanda7172
@sleepypanda7172 3 жыл бұрын
great explanation! this dry run really helped me a lot
@shubhamkale735
@shubhamkale735 3 жыл бұрын
Thank you sir for this dry run feeling lucky to find this video ... Keep making such dry run video sir Thank you
@nimishbajaj3815
@nimishbajaj3815 8 жыл бұрын
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 !
@biboswanroy6699
@biboswanroy6699 4 жыл бұрын
In the recursive solution isn't anyway space is consumed due to stacks
@MIRAZIB
@MIRAZIB 4 жыл бұрын
yes, obviously .. space complexity is O( h ) .. worst case will occur when the tree is skewed and the ancestor lies beneath of it.
@harshitpruthi5744
@harshitpruthi5744 4 жыл бұрын
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
@SuperHARIS14
@SuperHARIS14 2 жыл бұрын
its given in the question that both n1 and n2 will always be there in the tree.
@antuancaraballo9691
@antuancaraballo9691 8 жыл бұрын
Love your explanations Tushar! You are awesome!
@14rucha
@14rucha 8 жыл бұрын
Awesome video! Your explanation is too good. I can understand recursion very easily through your video.
@mayankmittal6552
@mayankmittal6552 4 жыл бұрын
man, your hair style and explanation a true entertainer!!
@uRamPlus
@uRamPlus 4 жыл бұрын
Hahahaha !!! He’s cool tho
@nadiiaparsons6088
@nadiiaparsons6088 2 жыл бұрын
It's much clear now. Thanks a lot for your explanation.
@vuleenguyen
@vuleenguyen 5 жыл бұрын
Thanks a lot. You gave me the idea to solve this problem
@sase1017
@sase1017 3 жыл бұрын
Thanks for the great explanation
@sumedhaj9017
@sumedhaj9017 2 жыл бұрын
Very nice explanation with the examples!
@sureshgarine
@sureshgarine 2 жыл бұрын
hi Thushar, so nice of you...explained very well
@levtunik997
@levtunik997 4 жыл бұрын
great explanation, you have high-quality videos..thanks.
@AP-eh6gr
@AP-eh6gr 8 жыл бұрын
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
@hemanthp2367
@hemanthp2367 7 жыл бұрын
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.
@mrdude1084
@mrdude1084 5 жыл бұрын
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
@nishantharava9181
@nishantharava9181 5 жыл бұрын
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.
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
this algo is based ln the assumption that that both nodes will be present in the tree...nice observation btw
@abhinavmishra7617
@abhinavmishra7617 4 жыл бұрын
4 years ago....damn
@HieuLe-mh4bc
@HieuLe-mh4bc 2 жыл бұрын
Great explanation! Thank you for the video.
@anandsakthivel9425
@anandsakthivel9425 7 жыл бұрын
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
@kfliden
@kfliden 3 жыл бұрын
Great explanation as usual!
@shreyaa_21
@shreyaa_21 4 жыл бұрын
Recursion Explained nicely, thanks
@stonecoldcold2941
@stonecoldcold2941 4 жыл бұрын
Awsome Explanation Bro!!
@444not
@444not 3 жыл бұрын
Thank you for the explanation. Very helpful.
@trinityartworld9732
@trinityartworld9732 3 жыл бұрын
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-yz5qy
@rohitkumar-yz5qy 6 жыл бұрын
you have given best explanation..thanks a lot
@shubham709
@shubham709 5 жыл бұрын
Great job, the explanation is very clear and lucid. Thanks a lot :).
@shubhamtalks9718
@shubhamtalks9718 4 жыл бұрын
Amazing explanation!!!
@gourab469
@gourab469 2 жыл бұрын
Excellent explanation! Hats off!
@nikkis8102
@nikkis8102 7 жыл бұрын
Thank you so much, Tushar!!
@RoshanKumar-nx6rk
@RoshanKumar-nx6rk 3 жыл бұрын
Your second algorithm has also O(n) space complexity!!! You can't neglect the function stack calls.
@reassume4826
@reassume4826 2 жыл бұрын
above & beyond awesomeness!
@hmoiz52
@hmoiz52 6 жыл бұрын
Very nicely explained! Thanks.
@chrisy.703
@chrisy.703 2 жыл бұрын
best lca video in youtube so far!
@GowthamBharadwaj
@GowthamBharadwaj 3 жыл бұрын
Very lucid explanation
@learnsharegrow7950
@learnsharegrow7950 3 жыл бұрын
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; }
@RightMeow28
@RightMeow28 3 жыл бұрын
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.
@Sameer88530
@Sameer88530 5 жыл бұрын
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-sn4bs
@shreyasingh-sn4bs 7 жыл бұрын
I have become a fan! Great work Sir :)
@rhondawang2639
@rhondawang2639 3 жыл бұрын
Thank you so much!
@aloksharma3480
@aloksharma3480 6 жыл бұрын
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.
@samratdr
@samratdr 4 жыл бұрын
Thank you for the video. One feedback I have is that the mic is too hard for the ears
@evantheking
@evantheking 7 жыл бұрын
finally good explanation. I understand it now
@whyarewehere9555
@whyarewehere9555 5 жыл бұрын
Thank you sir
@bumbarumbado
@bumbarumbado 8 жыл бұрын
beautiful explanation!
@nitinagrawal6637
@nitinagrawal6637 4 жыл бұрын
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.6816
@mehrdadk.6816 4 жыл бұрын
complexity can be O(logn) since it is a binary tree
@xxbighotshotxx
@xxbighotshotxx 7 жыл бұрын
Thank you for the explanation! I was able to write the code for it myself as a result!
@bipul2138
@bipul2138 4 жыл бұрын
Genius approach...well explained
@atulavhad1661
@atulavhad1661 7 жыл бұрын
@ 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.
@prathashukla6596
@prathashukla6596 5 жыл бұрын
@tushar can you cover this edge case in your algo.
@AKHILESHKUMAR-nk2rk
@AKHILESHKUMAR-nk2rk 4 жыл бұрын
it is assumed that the two nodes are present in the tree
@ShaliniNegi24
@ShaliniNegi24 4 жыл бұрын
Very nice Explanation.
@carloslaguna7036
@carloslaguna7036 8 жыл бұрын
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.
@haoli2976
@haoli2976 2 жыл бұрын
Genius. Thank you very much.
@adampatrick7687
@adampatrick7687 4 жыл бұрын
Really really helpful.
@ayushpalak
@ayushpalak 8 жыл бұрын
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
@mohammedghabyen721
@mohammedghabyen721 3 жыл бұрын
Great explanation, Thanks a lot
@GaneshSatputeAtPlus
@GaneshSatputeAtPlus 4 жыл бұрын
Here we assume that both nodes are present. If one of them is not present, we return a wrong output.
@madiyar8079
@madiyar8079 5 жыл бұрын
amazing!! Thanks Tushar! PLease continue!
@chetansarnad1232
@chetansarnad1232 4 жыл бұрын
Thanks for the video, really helpful
@BhatiJhabarSingh
@BhatiJhabarSingh 3 жыл бұрын
Excellent Explaination
@jubiliudu
@jubiliudu 2 жыл бұрын
Your solutions are amazing!! Keep doing it :)
@MLAICS
@MLAICS 5 жыл бұрын
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-
@0xeb- 3 жыл бұрын
Perfect. Thank you.
@HoanNguyen-fc8vb
@HoanNguyen-fc8vb 2 жыл бұрын
Very nice. Thank you very much.
@volodymyrkot6277
@volodymyrkot6277 5 жыл бұрын
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.
@RottenWoodInPower
@RottenWoodInPower 4 жыл бұрын
Very clear. Thanks
@jaspertienny8976
@jaspertienny8976 8 жыл бұрын
The amazing Tushar. Thanks!
@rajusaratkar2599
@rajusaratkar2599 8 жыл бұрын
Very nice and Clear Explanation ..Thanks for the Video.
@shivampandey8837
@shivampandey8837 3 жыл бұрын
No bro
@shimul6680
@shimul6680 2 жыл бұрын
Thank you very much.....! Super solution!!!
@harshkanani6605
@harshkanani6605 2 жыл бұрын
Very nice explanation thank you sir
@jacobstech1777
@jacobstech1777 3 жыл бұрын
i really love this guy
@umberto3271
@umberto3271 2 жыл бұрын
this is excellent, thank you
@SiddharthKulkarniN
@SiddharthKulkarniN 7 жыл бұрын
Great videos dude. Thanks!
@Balaji-cv5bz
@Balaji-cv5bz 4 жыл бұрын
Hey Tushar, wonderful explanation. can you please post a video of an inorder successor of a binary search tree. Thanks!
@adithiaa4385
@adithiaa4385 4 жыл бұрын
Great explanation! Thank you.!
@manishbhatt3614
@manishbhatt3614 8 жыл бұрын
@Tushar: Good work and good explanations.
@liangchen4439
@liangchen4439 4 жыл бұрын
A follow up of this question is to find the LCA of a list of nodes (more then 2) in this tree. Any ideas?
Iterative Postorder traversal of binary tree using one stack
14:05
Tushar Roy - Coding Made Simple
Рет қаралды 115 М.
Lowest Common Ancestor  - O(logN) | Binary Lifting
15:11
Fluent Algorithms
Рет қаралды 8 М.
Стойкость Фёдора поразила всех!
00:58
МИНУС БАЛЛ
Рет қаралды 6 МЛН
Good teacher wows kids with practical examples #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 8 МЛН
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Lowest Common Ancestor (LCA) Problem | Eulerian path method
17:02
WilliamFiset
Рет қаралды 44 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 402 М.
Binary Search : Median of two sorted arrays of different sizes.
24:48
Tushar Roy - Coding Made Simple
Рет қаралды 546 М.
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)
20:30
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 10 М.
How Binary Search Makes Computers Much, Much Faster
6:51
Tom Scott
Рет қаралды 1,4 МЛН
Fenwick Tree or Binary Indexed Tree
22:43
Tushar Roy - Coding Made Simple
Рет қаралды 237 М.
L27. Lowest Common Ancestor in Binary Tree | LCA | C++ | Java
14:09
take U forward
Рет қаралды 311 М.
Trie Data Structure
19:40
Tushar Roy - Coding Made Simple
Рет қаралды 411 М.
Стойкость Фёдора поразила всех!
00:58
МИНУС БАЛЛ
Рет қаралды 6 МЛН