/ tusharroy25 github.com/mis... github.com/mis... Find lowest common ancestor in binary search tree.
Пікірлер: 75
@ruitu60058 жыл бұрын
Awesome video, this solution is even more neat than the solution presented in "The Element of Programing Interview". Thanks!
@Simply_maniyaa8 жыл бұрын
Superb Solution. I tried modifying the solution for "Lowest Common Ancestor in Binary tree", but this ones even better
@TusharMudgal8 жыл бұрын
Hey tushar, can you please upload video about The Great Tree-List Recursion problem. I have been following your tree lectures and it is as always well explained except it misses one of the most asked interview problem.
@ruitu60058 жыл бұрын
this has my vote as well
@arvindhmani067 жыл бұрын
If Tushar can't convince Tushar then nobody can
@deepak-ui5li6 жыл бұрын
Thank You Tushar Roy you are an absolute beast
@davidenunin8864 жыл бұрын
Thanks it helped me a lot with the italian computer science olimpics
@itsnpe8 жыл бұрын
Hii, You have a very cool way of teaching advanced data structures and algorithms, why don't you now have a go a competitive coding , it will be a huge boom to peeps.
@_PRITIJHA3 жыл бұрын
Your Explanation is awesome
@harishdoddi3226 жыл бұрын
Awesome Tushar. Great videos!
@MrityunjayRanjan5 жыл бұрын
Small correction at first line-- > if(root.data > max(n1.data,n2.data))
@prekshakoirala63318 жыл бұрын
Damn. The best method ever.
@fabricator.cap.hill.seattle2 жыл бұрын
0:44 "looking for diverged in different paths and if one node is an ancestor of the..." But how the heck am I supposed to figure that out quickly in a coding interview?
@abhishekshaw98612 жыл бұрын
Made it really lucid , Sir😎
@dragoxofield8 жыл бұрын
Best explanation I have seen!
@manofacertainrage8567 жыл бұрын
Thank you for making these videos.
@ivsaimanoj8 жыл бұрын
Hi Tushar, nice explanation! One question - What if we got to check the existence of n1 and n2 in this tree? Consider they are not present, so this algo will return me an output. For ex: Node "-20" and Node "7" will return me Node "-10"..
@debajitsaikia18 жыл бұрын
Hey Tushar, Excellent explanation. Please upload some linked list related problems as well.
@ashusingh49377 жыл бұрын
I think the return value of function should be Node *, not Node. We are returning pointer to a node, not actual node.
@egor.okhterov7 жыл бұрын
ashu singh In Java 'Node' is a reference and he writes the code in Java.
@ernestina4757 жыл бұрын
Can you please do a lesson on finding LCA of Bst without the parent pointer reference?Thank you
@mcw08057 жыл бұрын
This doesn't consider the case where one or both of the elements doesn't exist in the tree. In case of the former, it will return the one that is present in the tree and not consider the other data (that DNE) at all.
@maaz35877 жыл бұрын
If the elements don't exist then how can their ancestor exist?
@egor.okhterov7 жыл бұрын
You check that condition before calling this function.
@niachungdupit81043 жыл бұрын
Since all the give example are either greater or lesser than root node 10 so we know where to move..but what if we had ,-10 and 30 then how we move
@kaushikjp8 жыл бұрын
Hi Tushar, You have condition like if (root > max (n1.date,n2.data)) , which max will return integer value and you are comparing with Node which is pointer so it will not compile code
@jahirulislammonir34908 жыл бұрын
In here may be if(root->data >(n1->data && n2->data)) will be in code.. But it's just for conception...
@kaushikjp8 жыл бұрын
Ya i got it. Thanks
@algorithmicthoughts85658 жыл бұрын
Thanks for this video very helpful. Can you please have a video explaining the O(n) time suffix array construction using Karkaainen. Thanks in advance.
@deepanshugarg47138 жыл бұрын
Hey Tushar, can you please make a video on treaps. Thanks
@eugeniaqing7 жыл бұрын
Thanks for your video, it's very helpful.
@SC-uv5le3 жыл бұрын
Cool and simple solution!
@judemartin83698 жыл бұрын
Fantastic as always !
@ivaylopankov73696 жыл бұрын
This solution assumes that both n1 and n2 are present in the tree, right? Maybe its worth mentioning.
@codeonmars5795 жыл бұрын
If n1 and n2 both are not present then still solution is valid. In that case nothing will be returned.
@kushaladhvaryu7 жыл бұрын
if(root.data > Math.max(n1.data, n2.data)){ return lowestCommonAncestor(root.left,n1,n2); }else if (root.data < Math.min(n1.data,n2.data)){ return lowestCommonAncestor(root.right,n1,n2); }else{ return root; } Here in conditions we will compare with root.data not just root as one is value other is node.
@angshumansarma28365 жыл бұрын
yup that correction should be there,but is quite mindful anyways...
@kautukraj4 жыл бұрын
Very helpful!
@Miss_iryna_8 жыл бұрын
Thanks Tushar! I like your explanation. Please make a video about quicksort.
@prekshakoirala63318 жыл бұрын
mycodeschool has a very good video on quicksort if you still need it.
@RafiqulIslam-je4zy4 жыл бұрын
Excellent. Thanks a lot.
@saptarshimitra12677 жыл бұрын
Nice and crisp. TY
@emersonmicu16838 жыл бұрын
Thanks alot, it's very easy to understand it.
@siddharthsethia59866 жыл бұрын
Keep doing the good work sir!!
@诸葛俊伟8 жыл бұрын
Tushar, Can you make a video of "Word Square" question in LeetCode, which I think is one of the most difficult question in LeetCode.
@arunvyas278 жыл бұрын
Hello Tushar, Very simple and elegant Algo, Thanks. Just curious, If i give input as 30,79: 79 not being in tree, I will get 30. Is this a valid answer?
@arunvyas278 жыл бұрын
+arun vyas Also need to do null check, or items not present in tree.
@arunvyas278 жыл бұрын
+Tushar Roy Thank you Tushar.
@AshutoshKumar-il3wk3 жыл бұрын
thanks explained so well !!!
@TheCowsrkewl8 жыл бұрын
This is really helpful!
@rashmithkoundinya7 жыл бұрын
Hey tushar the complexity for the solution is log N right where N is no. of nodes
@vitaly40977 жыл бұрын
Nope, this is O(N) solution as tree can be unbalanced, check out www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor for log solution.
@Bakepichai7 жыл бұрын
Short & sweet
@MoharnabSaikia8 жыл бұрын
Good job.Love ur videos :)
@MountCook7 жыл бұрын
Beautiful!
@MrThepratik4 жыл бұрын
precise solution
@jimmy16810006 жыл бұрын
Really appreciate!!!!!!!
@sivakishorejaladi34827 жыл бұрын
Awesome, simple super
@priyadarshanr2963 жыл бұрын
Why isn't the lowest common ancestor of (6,9) is -10
@nikhilkv5564 Жыл бұрын
-10 is also common ancestor of 6 and 9, but it’s not the lowest. 8 is the parent of of 6 and 9. Hence it would be the LCA.
@SaurabhSingh-db8xq6 жыл бұрын
Thanks bhaiya!
@pragyamukherjee82432 жыл бұрын
Thanks !
@deepakbammi39677 жыл бұрын
It should be root.data instead of root. :)
@ayushgarg59294 жыл бұрын
Yasss
@abhisekhagarwala95015 жыл бұрын
how does this code handles the last case i.e 30 and 78
@meikelk8825 жыл бұрын
It returns the root because it doesn't pass the condition at the node 30; (30)root < min(30, 78) since the minimum 30 isn't shorter but equal to 30.
@abhisheksharma35537 жыл бұрын
i THINK THERE IS A MISTAKE IN THE CODE.....It shouldnt be the max of both node's data... the root node should be less than both of the n1 and n2....... Node lca(Node node, int n1, int n2) { if (node == null) return null; // If both n1 and n2 are smaller than root, then LCA lies in left if (node.data > n1 && node.data > n2) return lca(node.left, n1, n2); // If both n1 and n2 are greater than root, then LCA lies in right if (node.data < n1 && node.data < n2) return lca(node.right, n1, n2); return node; } this is correct....
@akashreddy77252 жыл бұрын
Thanks
@TejasPatil-lu6zf8 жыл бұрын
cool
@chiquiro103 жыл бұрын
eres un papu we
@goloth7 жыл бұрын
10 is a common ancestor to 30 and 78, so the lowest common ancestor should be 10, not 30.