/ tusharroy25 github.com/mission-peace/inte... github.com/mission-peace/inte... Given two nodes find lowest common ancestor of these 2 nodes in the binary tree
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.
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
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
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...
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 !
Really helpful. Thanks a lot. And hoping to cover lots of more DS interview questions in future videos. 👍👍 And special thanks for the code. Bcoz most of got stuck into code part even after knowing the logic.
Thank you . Good one . You are awesome. from 10.55 the way you talked, looks like u are iterating an array ['like', 'share', 'subscibe'] + 'this video'
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.
Hi Tushar , Your videos has helped me alot . Thanks for the wonderful series . Sometimes i face problems while running through the call stack in recursion problem , i mean i find it difficult to get the flow . Could you pls make a video for that !!Thanks :)
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
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.
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
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.
Recursion is indeed a divine art
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.
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
@Kylan Ali instablaster =)
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!
Wow, great explanation about the algorithm! I appreciated the fact that you gave multiple examples that tested different things!
You are videos are just awesome! Great job! Can't imagine prep without your videos!
Thank you, sir! I was confused about this recursive code, but now I understand how it works perfectly!
Another great explanation with a beautiful solution!
This was a great visual walkthrough. Clear and concise.
After 5 years he's still helping
Yep
tera naam mast hai
@@prelimsiscoming thanks..
The space complexity of both the algorithms is O(height), because the recursive algorithm will have function calls on the stack.
I love this guy's multiple walking-through examples on white board!
Thank you for covering multiple cases. Other videos didn’t explain as thoroughly as you and didn’t go through many test cases
Love your explanations Tushar! You are awesome!
This is a very useful, well explained, and simple algorithm. Thank you, Tushar.
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
Great job Tushar! Absolutely awesome walkthrough..
Your explanation always hits the spot. Thanks :)
Excellent video/explanation. Your videos make algorithms so much easier.
Thank you! Finally a video that helped me understand the recursive details of the various cases of binary tree
Can't agree more
Thank you for the explanation! I was able to write the code for it myself as a result!
Awesome video! Your explanation is too good. I can understand recursion very easily through your video.
Great job, the explanation is very clear and lucid. Thanks a lot :).
Best Video on LCA available on CZcams, Thanks Tushar!
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...
Yes u r correct
The amazing Tushar. Thanks!
Thank you so much, Tushar!!
amazing!! Thanks Tushar! PLease continue!
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.
I really enjoy your video, it's so clear and helpful, many thanks!!!!!
I have become a fan! Great work Sir :)
@Tushar: Good work and good explanations.
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 !
Really helpful. Thanks a lot. And hoping to cover lots of more DS interview questions in future videos. 👍👍 And special thanks for the code. Bcoz most of got stuck into code part even after knowing the logic.
great explanation! this dry run really helped me a lot
Wow! Great explanation!
Very nicely explained! Thanks.
The other solution also takes the implicit stack during recursion, so yes it does require the extra space.
Thanks a lot. You gave me the idea to solve this problem
Your solutions are amazing!! Keep doing it :)
you have given best explanation..thanks a lot
Very nice explanation with the examples!
Great explanation! Thank you for the video.
Very nice and Clear Explanation ..Thanks for the Video.
No bro
great explanation, you have high-quality videos..thanks.
Great explanation as usual!
Nice examples to illustrate...thanks a lot buddy :-)
hi Thushar, so nice of you...explained very well
Clear and straight to the point.
thank you so much this is the most helpful video ive found for this question
Excellent explanation! Hats off!
finally good explanation. I understand it now
Excellence! Real knowledge, thank you.
Thank you sir for this dry run feeling lucky to find this video ... Keep making such dry run video sir Thank you
Great videos dude. Thanks!
Nicely explained. This is a popular problem statement in Amazon interviews.
It's much clear now. Thanks a lot for your explanation.
Thank you for the explanation. Very helpful.
beautiful explanation!
Thank you very much. this is great!
Salute to u Man !! _/\_ U are a blessing :)
Thanks for the great explanation
Thank you . Good one . You are awesome. from 10.55 the way you talked, looks like u are iterating an array ['like', 'share', 'subscibe'] + 'this video'
above & beyond awesomeness!
Amazing explanation!!!
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.
ASSUMTION:
We may assume that either both n1 and n2 are present in the tree or none of them are present.
NOTE : optimisation not works for special case
Recursion Explained nicely, thanks
best lca video in youtube so far!
Awsome Explanation Bro!!
nice, cool stuff u r doing !
Thanks for the video, really helpful
Great explanation...thanks
Thank u very much sir ... for ur explanation
Great explanation! Thank you.!
Great explanation, Thanks a lot
Genius approach...well explained
Hi Tushar , Your videos has helped me alot . Thanks for the wonderful series . Sometimes i face problems while running through the call stack in recursion problem , i mean i find it difficult to get the flow . Could you pls make a video for that !!Thanks :)
Awesome Explanation!
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
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.
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
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.
this algo is based ln the assumption that that both nodes will be present in the tree...nice observation btw
4 years ago....damn
Six years later and im still learning from you
Last example is really important to explain both nodes in the same path.
Nice explanation, thank you sir.
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
Really really helpful.
Very nice Explanation.
Tushar is the best!
just wondering, if i could possibly come up with this optimized soln in an actual interview...
Could you please explain also the Lowest Common Ancestor in general trees using sparse table or RMQ :)
Genius. Thank you very much.
In the recursive solution isn't anyway space is consumed due to stacks
yes, obviously .. space complexity is O( h ) .. worst case will occur when the tree is skewed and the ancestor lies beneath of it.
Very nice. Thank you very much.
God bless you man!
Very clear. Thanks
Perfect. Thank you.
Thank you very much.....! Super solution!!!
Thank you so much!
Helpful! Thanks.
this is excellent, thank you
nicely explained !!!
Excellent Explaination
simplest explanation i have ever seen.. awesome _/\_
man, your hair style and explanation a true entertainer!!
Hahahaha !!! He’s cool tho