Lowest Common Ancestor Binary Search Tree

  Рет қаралды 76,111

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

Күн бұрын

/ tusharroy25
github.com/mis...
github.com/mis...
Find lowest common ancestor in binary search tree.

Пікірлер: 75
@ruitu6005
@ruitu6005 8 жыл бұрын
Awesome video, this solution is even more neat than the solution presented in "The Element of Programing Interview". Thanks!
@Simply_maniyaa
@Simply_maniyaa 8 жыл бұрын
Superb Solution. I tried modifying the solution for "Lowest Common Ancestor in Binary tree", but this ones even better
@TusharMudgal
@TusharMudgal 8 жыл бұрын
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.
@ruitu6005
@ruitu6005 8 жыл бұрын
this has my vote as well
@arvindhmani06
@arvindhmani06 7 жыл бұрын
If Tushar can't convince Tushar then nobody can
@deepak-ui5li
@deepak-ui5li 6 жыл бұрын
Thank You Tushar Roy you are an absolute beast
@davidenunin886
@davidenunin886 4 жыл бұрын
Thanks it helped me a lot with the italian computer science olimpics
@itsnpe
@itsnpe 8 жыл бұрын
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.
@_PRITIJHA
@_PRITIJHA 3 жыл бұрын
Your Explanation is awesome
@harishdoddi322
@harishdoddi322 6 жыл бұрын
Awesome Tushar. Great videos!
@MrityunjayRanjan
@MrityunjayRanjan 5 жыл бұрын
Small correction at first line-- > if(root.data > max(n1.data,n2.data))
@prekshakoirala6331
@prekshakoirala6331 8 жыл бұрын
Damn. The best method ever.
@fabricator.cap.hill.seattle
@fabricator.cap.hill.seattle 2 жыл бұрын
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?
@abhishekshaw9861
@abhishekshaw9861 2 жыл бұрын
Made it really lucid , Sir😎
@dragoxofield
@dragoxofield 8 жыл бұрын
Best explanation I have seen!
@manofacertainrage856
@manofacertainrage856 7 жыл бұрын
Thank you for making these videos.
@ivsaimanoj
@ivsaimanoj 8 жыл бұрын
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"..
@debajitsaikia1
@debajitsaikia1 8 жыл бұрын
Hey Tushar, Excellent explanation. Please upload some linked list related problems as well.
@ashusingh4937
@ashusingh4937 7 жыл бұрын
I think the return value of function should be Node *, not Node. We are returning pointer to a node, not actual node.
@egor.okhterov
@egor.okhterov 7 жыл бұрын
ashu singh In Java 'Node' is a reference and he writes the code in Java.
@ernestina475
@ernestina475 7 жыл бұрын
Can you please do a lesson on finding LCA of Bst without the parent pointer reference?Thank you
@mcw0805
@mcw0805 7 жыл бұрын
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.
@maaz3587
@maaz3587 7 жыл бұрын
If the elements don't exist then how can their ancestor exist?
@egor.okhterov
@egor.okhterov 7 жыл бұрын
You check that condition before calling this function.
@niachungdupit8104
@niachungdupit8104 3 жыл бұрын
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
@kaushikjp
@kaushikjp 8 жыл бұрын
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
@jahirulislammonir3490
@jahirulislammonir3490 8 жыл бұрын
In here may be if(root->data >(n1->data && n2->data)) will be in code.. But it's just for conception...
@kaushikjp
@kaushikjp 8 жыл бұрын
Ya i got it. Thanks
@algorithmicthoughts8565
@algorithmicthoughts8565 8 жыл бұрын
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.
@deepanshugarg4713
@deepanshugarg4713 8 жыл бұрын
Hey Tushar, can you please make a video on treaps. Thanks
@eugeniaqing
@eugeniaqing 7 жыл бұрын
Thanks for your video, it's very helpful.
@SC-uv5le
@SC-uv5le 3 жыл бұрын
Cool and simple solution!
@judemartin8369
@judemartin8369 8 жыл бұрын
Fantastic as always !
@ivaylopankov7369
@ivaylopankov7369 6 жыл бұрын
This solution assumes that both n1 and n2 are present in the tree, right? Maybe its worth mentioning.
@codeonmars579
@codeonmars579 5 жыл бұрын
If n1 and n2 both are not present then still solution is valid. In that case nothing will be returned.
@kushaladhvaryu
@kushaladhvaryu 7 жыл бұрын
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.
@angshumansarma2836
@angshumansarma2836 5 жыл бұрын
yup that correction should be there,but is quite mindful anyways...
@kautukraj
@kautukraj 4 жыл бұрын
Very helpful!
@Miss_iryna_
@Miss_iryna_ 8 жыл бұрын
Thanks Tushar! I like your explanation. Please make a video about quicksort.
@prekshakoirala6331
@prekshakoirala6331 8 жыл бұрын
mycodeschool has a very good video on quicksort if you still need it.
@RafiqulIslam-je4zy
@RafiqulIslam-je4zy 4 жыл бұрын
Excellent. Thanks a lot.
@saptarshimitra1267
@saptarshimitra1267 7 жыл бұрын
Nice and crisp. TY
@emersonmicu1683
@emersonmicu1683 8 жыл бұрын
Thanks alot, it's very easy to understand it.
@siddharthsethia5986
@siddharthsethia5986 6 жыл бұрын
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.
@arunvyas27
@arunvyas27 8 жыл бұрын
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?
@arunvyas27
@arunvyas27 8 жыл бұрын
+arun vyas Also need to do null check, or items not present in tree.
@arunvyas27
@arunvyas27 8 жыл бұрын
+Tushar Roy Thank you Tushar.
@AshutoshKumar-il3wk
@AshutoshKumar-il3wk 3 жыл бұрын
thanks explained so well !!!
@TheCowsrkewl
@TheCowsrkewl 8 жыл бұрын
This is really helpful!
@rashmithkoundinya
@rashmithkoundinya 7 жыл бұрын
Hey tushar the complexity for the solution is log N right where N is no. of nodes
@vitaly4097
@vitaly4097 7 жыл бұрын
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.
@Bakepichai
@Bakepichai 7 жыл бұрын
Short & sweet
@MoharnabSaikia
@MoharnabSaikia 8 жыл бұрын
Good job.Love ur videos :)
@MountCook
@MountCook 7 жыл бұрын
Beautiful!
@MrThepratik
@MrThepratik 4 жыл бұрын
precise solution
@jimmy1681000
@jimmy1681000 6 жыл бұрын
Really appreciate!!!!!!!
@sivakishorejaladi3482
@sivakishorejaladi3482 7 жыл бұрын
Awesome, simple super
@priyadarshanr296
@priyadarshanr296 3 жыл бұрын
Why isn't the lowest common ancestor of (6,9) is -10
@nikhilkv5564
@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-db8xq
@SaurabhSingh-db8xq 6 жыл бұрын
Thanks bhaiya!
@pragyamukherjee8243
@pragyamukherjee8243 2 жыл бұрын
Thanks !
@deepakbammi3967
@deepakbammi3967 7 жыл бұрын
It should be root.data instead of root. :)
@ayushgarg5929
@ayushgarg5929 4 жыл бұрын
Yasss
@abhisekhagarwala9501
@abhisekhagarwala9501 5 жыл бұрын
how does this code handles the last case i.e 30 and 78
@meikelk882
@meikelk882 5 жыл бұрын
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.
@abhisheksharma3553
@abhisheksharma3553 7 жыл бұрын
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....
@akashreddy7725
@akashreddy7725 2 жыл бұрын
Thanks
@TejasPatil-lu6zf
@TejasPatil-lu6zf 8 жыл бұрын
cool
@chiquiro10
@chiquiro10 3 жыл бұрын
eres un papu we
@goloth
@goloth 7 жыл бұрын
10 is a common ancestor to 30 and 78, so the lowest common ancestor should be 10, not 30.
@nishithakur424
@nishithakur424 7 жыл бұрын
gr8
@adarshsharmanit356
@adarshsharmanit356 4 жыл бұрын
MNNITIANS like Here
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
Lowest Common Ancestor (LCA) Problem | Eulerian path method
17:02
WilliamFiset
Рет қаралды 45 М.
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,5 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Check if Binary Tree is Binary Search Tree
10:29
Tushar Roy - Coding Made Simple
Рет қаралды 122 М.
Skyline Problem
22:54
Tushar Roy - Coding Made Simple
Рет қаралды 194 М.
L47. LCA in Binary Search Tree
8:06
take U forward
Рет қаралды 132 М.
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 147 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236
12:48
Graph Theory - Lowest Common Ancestor (Arabic)
23:12
Arabic Competitive Programming
Рет қаралды 3,4 М.
L27. Lowest Common Ancestor in Binary Tree | LCA | C++ | Java
14:09
take U forward
Рет қаралды 327 М.
Lowest Common Ancestor of a binary tree | Leetcode #236
17:29
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН