Тёмный

Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach) 

Back To Back SWE
Подписаться 240 тыс.
Просмотров 83 тыс.
50% 1

Free 5-Day Mini-Course: backtobackswe.com
Try Our Full Platform: backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given the root of a binary tree and 2 references of nodes that are in the binary tree, find the lowest common ancestor of the 2 nodes. The nodes do not have parent pointers.
The Approach
So there are many modulations of this problem where you can build a hashtable and make parent pointers, etc. We will focus on the recursive solution.
The Algorithm
The key is that we want to root ourselves at a node and then search left and then right for either of the 2 nodes given.
If we see either node, we will return it, if we do not find the node in a subtree search the value of null will be returned and bubbled up.
After we search both left and right we ask ourselves what our results mean.
If we found nothing to the left, we just bubble up what is on the right (whatever that search result may be). This node we sit at cannot be the LCA since the left and right did not yield the 2 nodes we want.
If we found nothing to the right, we just bubble up what is on the left (whatever that search result may be). This node we sit at cannot be the LCA since the left and right did not yield the 2 nodes we want.
If both the right and left result are not null, we have found our LCA.
Why?
We know it is an ancestor at the least but we definitely know it is the lowest common ancestor because we went bottom upwards, whatever we hit will be the LCA and it will bubble up.
Complexities
Time: O( n )
We will be touching the whole tree in the search, there are n nodes in the tree and we do O(1) work at each node. There are not exactly n calls though but I need to double check this...I need to solve the recurrence but oh well...we know it will stay linear in the asymptotic regard.
Space: O( h )
Stack usage at maximum will be the trees height. Worst case would be O(n) if our tree is skewed purely to the left or right and we need to find deep nodes. But n IS h in that case. But we say O( n ) in that case since it is more accurate to what is happening, the tree's size in nodes dominating the height.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: / @hackerrankofficial
Tuschar Roy: / tusharroy2525
GeeksForGeeks: / @geeksforgeeksvideos
Jarvis Johnson: / vsympathyv
Success In Tech: / @successintech

Наука

Опубликовано:

 

10 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 284   
@BackToBackSWE
@BackToBackSWE 5 лет назад
Table of Contents: The Problem Introduction 0:00 - 0:33 Getting Good At Tree/Recursive Problems 0:49 - 2:51 What We Will Learn In This Video 2:51 - 3:34 Case #1: The Common Case 3:34 - 5:27 Case #2: The Overlap Case 5:27 - 6:47 The Key To Recursion. Reduction To A Single Focus. 6:47 - 7:55 Defining Our Recursive "Policy" At A Single Node 7:55 - 9:15 Policy #1: Both Found 9:15 - 9:32 Policy #2: Either Found (but not both) 10:48 - 12:51 Live Code Run-through 12:51 - 16:05 Time Complexity 16:05 - 17:15 Space Complexity 17:15 - 18:50 Wrap Up 18:50 - 20:10 Mistakes: 9:34 -> I meant "if we find x on the right", not "if we find x on the left" 16:35 -> We don't have a "potential" to touch the whole tree, we will touch the whole tree with this approach. Even if the LCA is found in say...the left subtree of a node...it will still make an LCA call to its right and of course get 'null' back since both nodes were already found in the left subtree and an LCA was already reaped. Before You Say It: For the 2nd base case we do value comparisons and this can present a problem if the value at a node we are searching for is repeated in the tree. This can simply be converted into a reference check since in the problem (at the time this video was made) passes us the reference to the 2 nodes we are searching for an LCA for. References in memory will always be unique per node. The code for this approach to the LCA problem as applied to Binary Trees is in the description. Fully commented for teaching purposes.
@huynhduc9
@huynhduc9 Месяц назад
Hi man, where is the code? It's not in the description.
@laurakraman2737
@laurakraman2737 5 лет назад
You make me feel normal. That's a compliment. All week I've been feeling like I'm never gonna get it.
@BackToBackSWE
@BackToBackSWE 5 лет назад
You are normal. These problems just suck. Stick with it. If I can do this, anyone can.
@philipdimeski5188
@philipdimeski5188 4 года назад
Glad I'm not the only one!
@GvSharmaBKP
@GvSharmaBKP 3 года назад
This guy is really kind
@ajayunni6361
@ajayunni6361 5 лет назад
I just want to say that your videos has actually helped me immensely especially solving tree problems. Your approach of taking a tree problem and then thinking it based on one single node is priceless. A thousand thanks to you man! Today I landed my dream job! Anyone who is reading this, do watch all his videos, they are awesome.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Oh nice!!! Glad you are happy. Wish you the best - your internet friend Ben
@BharCode09
@BharCode09 4 года назад
Before one understands Recursion, one must first understand Recursion! I know, that's a joke, but it hurts.... Very bad.. Very deep!
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@_sudipidus_
@_sudipidus_ 4 года назад
Your joke overflows.. :P Improved version:=> Before one understands recursion one must first understand recursion until he or she understands recursion :)
@amitbajpai6265
@amitbajpai6265 3 года назад
Sorry but maximum recursion limit has been reached .
@akshitarora470
@akshitarora470 4 года назад
This was a great explanation. I have been watching all your videos, a couple of them every day, coding them out later, and trying to make myself good enough for constructing a logical thinking for these interview questions. No tutor on internet teaches via giving the intuitive approach, and that is what sets you apart, and makes you stand out! You really do a wonderful job! Thanks a lot!
@BackToBackSWE
@BackToBackSWE 4 года назад
great good luck
@louisehsu8480
@louisehsu8480 4 года назад
Thank you! I solved this initially using two stacks, but the runtime was pretty abysmal. This makes the optimal solution much more clear!
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@manogyapulivendala2504
@manogyapulivendala2504 3 года назад
Damn, this is THE VIDEO to watch when you're starting out with Recursion and Trees. You're awesome man!
@BackToBackSWE
@BackToBackSWE 3 года назад
ye
@abdullahzaidan7394
@abdullahzaidan7394 4 года назад
it looks like you have spend a lot of time going deep into the concepts to make these type of problems easy for us. Thanks for such hard work. Hope your content gets more views.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks for watching
@mrallenchuang
@mrallenchuang 5 лет назад
Keep it up, been watching you for over a month and already feel like i've learned so much!
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@deepaksarvepalli2344
@deepaksarvepalli2344 3 года назад
you gave me a chance to make myself better than yesterday...
@prithazz
@prithazz 4 года назад
This is the best video on recursion I have watched. Still not able to fully apply recursion to other problems but this makes a lot of sense how to "read" the function when the results from bottom come up and hit base.
@BackToBackSWE
@BackToBackSWE 4 года назад
Nice and it just takes time.
@SelftaughtSoftwareEngineer
@SelftaughtSoftwareEngineer 3 года назад
I really liked that you include the code and walk us through it in this video. Thank you for sharing!
@BackToBackSWE
@BackToBackSWE 3 года назад
sure
@marcusrobinson5516
@marcusrobinson5516 5 лет назад
Thanks for this video man, getting me to try and understand recursion a little better at least
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@aidardarmesh8024
@aidardarmesh8024 4 года назад
Best explanation and great advices not from senior-shmenior, but for usual human and in human language. Thanks!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@chhaviagarwal7514
@chhaviagarwal7514 4 года назад
The way you explained each and every thing related to a problem is just amazing
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@alammahtab08
@alammahtab08 4 года назад
Great explanation, some important things that I think are worth mentioning here. I think the implementation is actually a bit wrong. For example for the below binary tree, lets say we want to find out the LCA of nodes 5 and 6. With the implementation shown in this video, the left recursive call will satisfy the base case at node 5, because node 5's value matches, so it will return the Node 5 without trying to find the match for Node 6 in the left subtree. 3 / \ 5 1 / \ / \ 6 2 0 8 / \ 7 4 So to really start the evaluation from bottom of the tree to top, the recursive calls should be done first, before checking the Node's value. Below is the implementation : /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; TreeNode leftSearchResult = lowestCommonAncestor(root.left, p, q); TreeNode rightSearchResult = lowestCommonAncestor(root.right, p, q); if (root.val == p.val || root.val == q.val) return root; if(leftSearchResult != null && rightSearchResult!= null ) return root; return leftSearchResult != null? leftSearchResult : rightSearchResult; } } The leetcode question guarantees that both nodes p and q will always exist in the binary tree. But If only one node exist in the tree then above implementation will return the node that matched the value in the binary tree. Which is kind of wrong, ideally it should have returned null because we were not able to find both the nodes in the tree. =============== Follow up question ========= What if we are not guaranteed that both nodes p and q will exist in the binary tree? Approach : We are going to take two boolean properties firstNodeExist and secondNodeExist, and when we find that first or the second node exist in the binary tree, we will set the appropriate boolean property to true. This way we can find out whether both the values exist in the tree or not, and if both the values are not found in the binary tree we return null as LCA from the lowestCommonAncestor method. Implementation : class Solution { public boolean firstNodeExist; public boolean secondNodeExist; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; TreeNode lca = lca(root, p, q); return (firstNodeExist && secondNodeExist)? lca : null; } public TreeNode lca(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; TreeNode leftSearchResult = lca(root.left, p, q); TreeNode rightSearchResult = lca(root.right, p, q); if (root.val == p.val) { firstNodeExist = true; return root; } else if(root.val == q.val) { secondNodeExist = true; return root; } if(leftSearchResult != null && rightSearchResult!= null ) return root; return leftSearchResult != null? leftSearchResult : rightSearchResult; } } I made some notes here github.com/eMahtab/lowest-common-ancestor-of-a-binary-tree , you might find it helpful.
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@anmolmishra1914
@anmolmishra1914 5 лет назад
Thanks a lot. Working on my recursion.
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@lonelyloser69
@lonelyloser69 4 года назад
The way you explain concepts is exceptional! Keep up the great work!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@DavisBelisle
@DavisBelisle Месяц назад
This is the best video about trees I have ever seen. Thank you so much!
@pyak6761
@pyak6761 3 года назад
you will be a big reason why i get a job at the big boys in six months if i make it. bro this content is so gooooold cause you actually break it down. i don't even need to see the code cause the concepts are so good dam
@BackToBackSWE
@BackToBackSWE 3 года назад
great - live a good life
@prajakta_patil
@prajakta_patil 5 лет назад
Great Explanation :) Appreciate your hard work !!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
sure
@aamirjamal6833
@aamirjamal6833 5 лет назад
There is no chance that I would be able to think this out in an interview by myself. Anyways, thanks for the lucid explanation.
@BackToBackSWE
@BackToBackSWE 5 лет назад
yeah, me neither
@wearegeeks
@wearegeeks 3 года назад
Those few words of the beginning. Thank you man, I'm still struggling with most of the problems I encounter. I thought I was stupid or something.
@adityasrivastava7956
@adityasrivastava7956 3 года назад
hola bro wassup
@lakshmimanasamvs7833
@lakshmimanasamvs7833 4 года назад
thank you so much!! I finally understood this after watching your video. I was struggling to understand this before watching your video.
@BackToBackSWE
@BackToBackSWE 4 года назад
great
@trushapatel9012
@trushapatel9012 4 года назад
Your explanations are very good. Thanks! Preparing for Amazon On Site Interview from here.
@BackToBackSWE
@BackToBackSWE 4 года назад
nice, good luck
@darod6098
@darod6098 4 года назад
You're the best explaining. It's amazing.
@BackToBackSWE
@BackToBackSWE 4 года назад
no u
@rishabhkukreja3485
@rishabhkukreja3485 4 года назад
I finally understood the key behind solving recursive problems. Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
nice.
@danni6113
@danni6113 5 лет назад
Excellent explanation regarding the recursive call!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thx
@wishingbottle
@wishingbottle 4 года назад
love the whiteboard approach (for interviewing), thanks so much!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@PritamBanerjee999
@PritamBanerjee999 2 года назад
This is the best explanation I have found for this problem in the internet. Thank you
@adityasrivastava7956
@adityasrivastava7956 3 года назад
Best video. Thanks a lot!
@tathagatnegi5923
@tathagatnegi5923 4 года назад
This way of showing and explaining code is really great👍
@BackToBackSWE
@BackToBackSWE 4 года назад
great
@saptarshiganguly1683
@saptarshiganguly1683 3 года назад
Please keep making such videos more. It's really helpful.
@ekejma
@ekejma 3 года назад
He helps you to deeply understand the problem deeply. Very grateful to break up study sessions with his lectures.
@kirancoding1576
@kirancoding1576 5 лет назад
Am very scared looking at problem on trees and graphs but your explanation is making me understand them easily . thank you for all the videos on trees
@BackToBackSWE
@BackToBackSWE 5 лет назад
Give it time, they get easy
@blatogh1277
@blatogh1277 3 года назад
This videos is really helping to understand how this LCA works. The basic idea is first to find out is where are those two nodes are and second do the return and determining whether is common ancestor
@Piyushraj0
@Piyushraj0 Год назад
Loved the thinking behind it
@jacariboboye
@jacariboboye 5 лет назад
Great explanation! Thank you. Best of luck with your internship.
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@fayel.8911
@fayel.8911 2 года назад
Thank you so much for sharing. Is that possible we can see you solve LCA similar questions on Leetcode and compare what difference between all to them?
@josephgodwin8729
@josephgodwin8729 3 года назад
This is the best video on trees and recursion! on my way to solve all Tree questions on LC -->
@rajatsaxena2647
@rajatsaxena2647 5 лет назад
Thanks a lot for these detailed videos. How about the approach where you walk up the tree until you hit the root, for both nodes and create two arrays containing parents of the nodes. Then we can run a loop and keep on matching the parents from both these arrays and return when we’ve found a difference. That would represent the lowest common ancestor in my opinion.
@BackToBackSWE
@BackToBackSWE 5 лет назад
Yes. I'm familiar with this approach. We would need parent pointers for that.
@fares.abuali
@fares.abuali 2 года назад
Thank you so much 🧡
@yilei3563
@yilei3563 2 года назад
extremely helpful!!!
@amitrk23
@amitrk23 2 года назад
Great explanation. Thanks a ton man!
@aakashjain3498
@aakashjain3498 4 года назад
Saw you're linkedin. Can't believe you doing so much only when you're 21.
@BackToBackSWE
@BackToBackSWE 4 года назад
I'm 20 rn and nah, it is nothing. Anyone can do anything whenever (within biological, physics, & genetic constraints).
@indsonusharma
@indsonusharma 4 года назад
love it !!!!!!!!! Great Expl..
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@soojinlee2420
@soojinlee2420 5 лет назад
Thank you soooo much for the video, You explanation is really clear and intuitive :) If you looking for next subject for a video, can you do minimax? Me and my friend have a hard time to understand the minimax u.u
@BackToBackSWE
@BackToBackSWE 5 лет назад
maybe, I only take requests of Patrons for now to be fair to those supporting the project
@idemchenko-js
@idemchenko-js 4 года назад
Thank you for your great explanation. Awesome that you mentioned about focusing on a node. Imho, it is a bit clearer with languages that support sum types and pattern matching as you simply list all possible variants. However, I believe, the key question is still open. Why did you do `if (leftSearchRes == null) return rightSearchRes; if (rightSearchRes == null) return leftSearchRes;`. Everything else makes sense, but this is a key twist to the whole problem, I assume.
@BackToBackSWE
@BackToBackSWE 4 года назад
If both are null then the latter if statement still returns null for the search. Otherwise, it is a matter of passing up the result from a single successful subtree search.
@sher.5027
@sher.5027 2 года назад
Dude, You just made it so easy.
@neharikakhera6411
@neharikakhera6411 4 года назад
Great explaination !!
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@shobanadevi5517
@shobanadevi5517 Год назад
You are really great!
@nupurkohli4634
@nupurkohli4634 4 года назад
great explanation! keep up the good work
@BackToBackSWE
@BackToBackSWE 4 года назад
thx
@sammynochains3455
@sammynochains3455 4 года назад
Finalllllly .. a good video .. after searching recursively for 2 hrs.. 😁😁
@BackToBackSWE
@BackToBackSWE 4 года назад
nice
@mikedelta658
@mikedelta658 6 месяцев назад
Great explanation!
@bekaryskuralbay3751
@bekaryskuralbay3751 4 года назад
Great job!
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@digital2man
@digital2man 3 года назад
You make it sooo easy.
@voskigolf899
@voskigolf899 4 года назад
Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@charlottedu762
@charlottedu762 2 года назад
thanks!!
@mr_phamtastic
@mr_phamtastic 3 года назад
wow you're an excellent teacher!
@hooshmandali
@hooshmandali 4 года назад
Thanks for the great video. I think there is a typo in function names. The initial name is "lca", however in recursive calls, "search" function is called. One of them should be changed to match the other one. Again awesome job and good luck!
@BackToBackSWE
@BackToBackSWE 4 года назад
yeah noted, thanks
@harsha20jun
@harsha20jun 4 года назад
Great explanation. I really admire your videos. Got a question though. This algorithm assumes both the nodes we are finding exist in the tree somewhere. How would the algorithm change if we are not sure whether both of those exist in the tree?
@BackToBackSWE
@BackToBackSWE 4 года назад
I'm not sure, would have to think on this.
@tayeebhasan
@tayeebhasan 4 года назад
Thank you so much
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@user-oy4kf5wr8l
@user-oy4kf5wr8l 3 года назад
Thank u buddy☺️ u r amazing
@josephwong2832
@josephwong2832 3 года назад
love your videos bro!
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@roliverma4483
@roliverma4483 3 года назад
The best teacher out there :D
@vedantiyangar151
@vedantiyangar151 4 года назад
I encountered this problem for the first time in an interview for one of the Big Four. My dumb ass maintained an array of the references of the parents of each node to be found and compared the references to find a common point. No wonder they kicked me out. :(. Thanks to Ben, I will get MY REVENGE.
@BackToBackSWE
@BackToBackSWE 4 года назад
hahahahaha
@SunkuSai
@SunkuSai 4 года назад
Why is this a terrible solution? The time complexity is the same because each node is visited at most twice. The space complexity is O(n) instead of O(h) which is much worse for a balanced tree. But it's still a reasonable solution, right?
@vedantiyangar151
@vedantiyangar151 4 года назад
@@SunkuSai I agree, but I believe the other interviewees came up with this solution, and the interviewers had a limit or something.
@notyournormaldev1419
@notyournormaldev1419 4 года назад
@@SunkuSai can you explain more about your approach 🙏 which array you are talking about
@nicesacbro4891
@nicesacbro4891 4 года назад
​@@notyournormaldev1419 search for x , and store its path in an array. Do the same for y. Now start comparing those arrays, when you encounter different values at an index, the index before that stores the LCA.
@solletisuresh4277
@solletisuresh4277 4 года назад
Awesome, thank u for rich content
@BackToBackSWE
@BackToBackSWE 4 года назад
Sure
@VinothiniAnabayan
@VinothiniAnabayan 3 года назад
Amazing explanation:)
@sijiewu
@sijiewu 4 года назад
干的漂亮! Great explanation! Thank you!
@BackToBackSWE
@BackToBackSWE 4 года назад
sure
@ShubhamSingh-cc1bb
@ShubhamSingh-cc1bb 3 года назад
Elegant Explanation
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@jiazhengguo5493
@jiazhengguo5493 5 лет назад
Awesome!!!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
thanks
@sui8261
@sui8261 5 лет назад
Very clear, Nice job! Why there are so many guys on RU-vid can teach better than college professors...
@BackToBackSWE
@BackToBackSWE 5 лет назад
Hahaha, maybe we are college professors in disguise 👀👀🕵🕵 But in all seriousness, a student is better positioned to teach concepts to other students than a seasoned professor is because a student knows exactly how other students will get stuck. Aka...most esteemed professors do research and are very, very, brilliant. Much more than any outsider could comprehend. But...there is a coefficient of loss that teaching introduces to that intellect. A perfect lesson builds a perfect bridge from the student's current level of understanding, to the teacher's understanding. So for me...I'm not that smart, I'm not the best problem solver...but I can angle lessons in a way that will bring anyone into my mind and way of thinking better because I had to cross that bridge only 1 or 2 years ago. Many professors and teachers crossed their bridge 10's of years ago and they lose that connection to the student's plight. Long story short...teaching is a whole different skill and a hard one to get right. Regardless, I have the utmost respect for all those who have every taught me (both terrible and brilliant). It is the nature of things that make it this way. Very very very very hard to get right. Afterward: Dang...this is a wise comment I think...something I'd read on Reddit in retrospect.
@sui8261
@sui8261 5 лет назад
@@BackToBackSWE Agreed, that make sense. Anyways, I was confused about LRU cache implementation and LCA for a long time but fortunately I saw your video in my recommendation list today and I can now finally understand them...Thank you for your hard work and keep going!!
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@sui8261 Will do
@umapathybabu8397
@umapathybabu8397 4 года назад
Helped a lot, can you do some oo design questions?
@BackToBackSWE
@BackToBackSWE 4 года назад
yeah
@kedimnvfjnnvfjffurju
@kedimnvfjnnvfjffurju 2 года назад
Very good explanation
@neghatnazir1668
@neghatnazir1668 3 года назад
thanks man! you are awesome
@BackToBackSWE
@BackToBackSWE 3 года назад
thx
@coolnikrox92
@coolnikrox92 5 лет назад
Hey Nice explanation :). Could you make a video on Tries data structure and Segment Tree. Thanks :)
@BackToBackSWE
@BackToBackSWE 5 лет назад
I may but I only now take video suggestions from Patrons to be fair to the people monetarily supporting the project
@arnabbiswas2773
@arnabbiswas2773 4 года назад
Hey Benyam! Thanks a lot for this great explanation. I had some doubts regarding LCA but its all gone now. I just noticed a little typo in the pseudocode that you showed (the function name is lca() but you call search() for the leftSearchResult and rightSearchResult). Apart from that the video and the explanation were super easy to understand! Thank a lot!
@BackToBackSWE
@BackToBackSWE 4 года назад
I would look into this but too busy. Just want you to know I saw this and can't respond
@arnabbiswas2773
@arnabbiswas2773 4 года назад
@@BackToBackSWE Well it's just a small typo. Moreover you already have the correct code in GitHub.
@BackToBackSWE
@BackToBackSWE 4 года назад
@@arnabbiswas2773 ok
@jagr6741
@jagr6741 2 года назад
You're Goated!
@rajatsemwal1865
@rajatsemwal1865 3 года назад
Who are you, who are you so wise, in the ways of Computer Science? Well, memes apart, your explanation was really on point and easily understandable for a non-pro coder like me. I also get confuse and scared of recursion and tree problems. Thank you! All the best and stay safe!
@BackToBackSWE
@BackToBackSWE 3 года назад
thx and thx
@amangoyal3583
@amangoyal3583 3 года назад
Can u please tell why we return the left root if right is not having any node or we return right node if left is not having any node? 12:25
@AshokBathini
@AshokBathini 4 года назад
Great explanation & cool algo. There's a minor bug: If only one of the values (x or y) is present in the tree, instead of returning NULL, it returns the Node that contains the value x or y as the LCA.
@BackToBackSWE
@BackToBackSWE 4 года назад
The problem assumes both nodes are present
@ayushmishra3388
@ayushmishra3388 3 года назад
Hey this algorithm will only work for non duplicates values of the nodes whose LCA is to be find out? am i correct? wht i mean is lets suppose i am finding LCA for 5, 2. And i find multiple occurrences of 5 and 2, then this algo wont work right?
@yicai7
@yicai7 3 года назад
Genius!!!!
@cocoarecords
@cocoarecords 5 лет назад
Phenomenal
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks
@fables5091
@fables5091 Год назад
quick question, would we not return lca(root.right) and lca(root.left) as the function given isnt recursive
@edwinrosemond989
@edwinrosemond989 2 года назад
Hi there! is the code still in the description?
@likhithrkrishna4914
@likhithrkrishna4914 4 года назад
You’re the best
@BackToBackSWE
@BackToBackSWE 4 года назад
no u
@Gavy093
@Gavy093 3 года назад
Will this still work if one of the nodes that we are looking for is not present in the tree?
@victorwarner2734
@victorwarner2734 5 лет назад
Good channel
@BackToBackSWE
@BackToBackSWE 5 лет назад
thx
@gxyxy1012
@gxyxy1012 5 лет назад
had this on an exam recently lol thx
@BackToBackSWE
@BackToBackSWE 5 лет назад
nice
@dephc0n1
@dephc0n1 5 лет назад
Same for an interview. But worded different so realizing it's a lca is a major key.
@TheDEMMX
@TheDEMMX 4 года назад
really clean solution code
@BackToBackSWE
@BackToBackSWE 4 года назад
ye
@TheDEMMX
@TheDEMMX 4 года назад
@@BackToBackSWE Seriously, I have need researching various ways to solve LCA, this is by far the most elegant code. I noticed some of your solutions are from EPI, but EPI has a different recursive solution for this and the one you explain I love a lot more. Thanks!
@BackToBackSWE
@BackToBackSWE 4 года назад
@@TheDEMMX great
@b9944236
@b9944236 Год назад
I think the beginning talk is more precious than how to solve this problem.
@arielazoulay6553
@arielazoulay6553 2 года назад
great explenation where is the code ?
@taritgoswami3957
@taritgoswami3957 4 года назад
Great! sometimes you become too much abstract to understand. Please try to show at least one example with pseudo-code about which you are talking. Writing down the pseudo-code will help us to better understand. Thanks, keep making this type of videos :)
@BackToBackSWE
@BackToBackSWE 4 года назад
ok
@sidagarwal43
@sidagarwal43 4 года назад
Could you explain how can we do it for a graph or a n-ary tree. Thanks
@BackToBackSWE
@BackToBackSWE 4 года назад
yes
@Mrswapsam13
@Mrswapsam13 4 года назад
Great Explanation! Just had a doubt, shouldn't the search be the same recursive function? That is, search(root.left,x,y) should be a recursive call to lca(root.left,x,y)
@BackToBackSWE
@BackToBackSWE 4 года назад
I don't remember the code presented in this video.
@Quintenkonijn
@Quintenkonijn 5 лет назад
With the function search(root, x, y) you are referring to lca(root, x, y), right?
@BackToBackSWE
@BackToBackSWE 5 лет назад
yeah, it is just a name change. You are basically searching for occurrences of either x's val or y's val and bubbling up the appropriate result.
@Quintenkonijn
@Quintenkonijn 5 лет назад
Great! Thanks for getting back so quickly. Keep up the great videos like this one and I'm sure you will reach your goal before you know! Cheers :-) @@BackToBackSWE
@BackToBackSWE
@BackToBackSWE 5 лет назад
@@Quintenkonijn cheers
@willturner3440
@willturner3440 3 года назад
You think like my mind ☺
@vanyastaleva415
@vanyastaleva415 4 года назад
Brilliant! Thank you! In hakkerrank this is marked as an easy task but I don't think it is. I think it's medium.
@BackToBackSWE
@BackToBackSWE 4 года назад
thanks.
@emilyhuang2759
@emilyhuang2759 4 года назад
lol, the little ohmitofo at the end
@BackToBackSWE
@BackToBackSWE 4 года назад
what
@emilyhuang2759
@emilyhuang2759 4 года назад
The Buddhist monk gesture thing? idk
Далее
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Mansan oshdi😅
00:22
Просмотров 2,6 млн
Harder Drive: Hard drives we didn't want or need
36:47
Rust and RAII Memory Management - Computerphile
24:22
Просмотров 223 тыс.
Мой новый мега монитор!🤯
1:00