LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236

  Рет қаралды 40,177

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 44
@def__init
@def__init Жыл бұрын
I like when you quickly show the use case while coding, it helps solidify what case we're on and removes the need for us to rewind quickly. And tbh rarely do ppl ever figure out the approach then go straight to coding without ever looking back at their drawing / plan. Keep up the great work!
@LeeK301
@LeeK301 7 ай бұрын
This is really clever thinking with the part of "return l or r". I say this because I was approaching this problem w/ the mindset that we MUST find both nodes; but I see through your example that if we find one, and we cant find the other, we just assume that the node that was found is the LCA for both! Very nice...
@crackfaang
@crackfaang 7 ай бұрын
Yea it's definitely a cool little trick. Glad you found the video useful and learned something new. Keep up the grind 💪
@gothfrog69
@gothfrog69 8 ай бұрын
Thank you for making this problem make sense. Wow.... Much simpler than leetcode's "official" solution.
@syafzal273
@syafzal273 9 ай бұрын
You mentioned that you may not need the base case because we are guaranteed to have an LCA, but the base case is needed because its a recursive function and when we reach a left/right which is None, we need the base case to kick in.
@retrofacade9832
@retrofacade9832 6 ай бұрын
agree
@rachelwilliams8569
@rachelwilliams8569 2 ай бұрын
Thanks for the comment. I was going to mention the same thing.
@iswariyar9169
@iswariyar9169 2 жыл бұрын
just a Thank you is really not sufficient for this crystal clear explanation. Beyond Awesome
@shelllu6888
@shelllu6888 Жыл бұрын
Thank you! For the first time, I finally understood your explanation and able to code it out without looking at the solution for this problem!
@cloud15487
@cloud15487 11 ай бұрын
the reason you're adding the base case is not to convince the interview that the tree could be null, it's needed in any case if the node we're looking for isn't in the subtree. so it's not optional at all, the base case (if root == null return root) is required.
@vamsikrishnagannamaneni912
@vamsikrishnagannamaneni912 9 ай бұрын
yea as he didnt create a separate helper function , that condition is needed
@aleetsai8636
@aleetsai8636 Жыл бұрын
The way you explain the question is so amazing. It's really easy to understand. Thank you so much!
@energy-tunes
@energy-tunes 5 ай бұрын
space complexity should be o(h) where h is height of the tree since the call stack will hold at most h stack frames in recursive depth first search
@bhaveshsrivastava2112
@bhaveshsrivastava2112 2 жыл бұрын
Hi, Thanks for explanation! Can you tell whats the difference between this and #1650 of leetcode.
@crackfaang
@crackfaang 2 жыл бұрын
The inputs are different. In #1650 you are only given the nodes P and Q but not root. Also, in #1650 you are given the parent pointer of each node. So in this question you go from the root down, but in #1650 you go from the nodes P and Q up instead. I have a video on #1650 out as well. Make sure to watch that one 😉
@ebenezeracquah478
@ebenezeracquah478 Жыл бұрын
I do like your explanations, they are intuitive and clear. Thank you very much.
@jeongtaebang3679
@jeongtaebang3679 2 ай бұрын
Technically, this algorithm can also handle the case where both nodes are not present in the tree right? It just cannot handle the case where only one node is present in the tree?
@fadsa342
@fadsa342 Жыл бұрын
Any advice for coming up with base cases? I looked at this problem for a while and didn't come up with there only being three possibilities
@crackfaang
@crackfaang Жыл бұрын
The 3 cases are not really the bases cases, they're the main meat of the problem. A base case would be handling a NULL root or something similar. It mostly comes from experience and having seen many similar problems. Nothing wrong with not being able to see it from the first try. If you are able to have an "ah-ha" moment once you see the solution then you will likely remember it forever.
@jimmyahmed5424
@jimmyahmed5424 2 жыл бұрын
Thank you for explaining! but why do we need line 13 and 14?
@awa8766
@awa8766 2 жыл бұрын
You need these two lines in the case p or q are your root node. If they are your root node then it's your LCA instantly because it's your tree's very first level that's common to every other node.
@chowdhurylinianazmi5615
@chowdhurylinianazmi5615 2 жыл бұрын
@@awa8766 I don’t think it’s very first level. It’s a recursive call, so you may get a match of this at any level. The intent of that line is once a node found is equal to p (or q) we won’t go further down of that node in recursion. The other parts of the tree might have q. If not the very last condition makes this node as the LCA.
@awa8766
@awa8766 2 жыл бұрын
@@chowdhurylinianazmi5615 You are correct and your description is more accurate. When I explained it, I saw it from a level-order perspective, but the idea is the same. The first instance of a p or a q at a root instantly guarantees an LCA.
@АхтемВейс
@АхтемВейс 2 жыл бұрын
But what if your dfs returned 6 to you as one of the nodes and the other let’s say would be 4. You would return 6 in that case which is incorrect.
@leetcoderafeeq2641
@leetcoderafeeq2641 2 жыл бұрын
Line 19
@vamsikrishnagannamaneni912
@vamsikrishnagannamaneni912 9 ай бұрын
it would return the parent node that recieved 6 from left and 4 from right. Directions are implied as left is returned before right.
@mathsky4401
@mathsky4401 2 жыл бұрын
Beautifully explained. simplified solution and clear explanation. But why so low views?
@crackfaang
@crackfaang 2 жыл бұрын
Haha people haven’t caught on to the channel yet. There’s a lot of Leetcode channels on KZbin
@joebaldwin9005
@joebaldwin9005 Жыл бұрын
I have one question, what if p is at the bottom of the left subtree and q doesnt exist in the tree. This would return p which is technically not the common ancestor?
@crackfaang
@crackfaang Жыл бұрын
You should check the constraints listed in the question itself as I don’t recall off the top of my head but I’m pretty sure for this one both p and q are required to exist in the tree
@no3lcodes
@no3lcodes Жыл бұрын
@@crackfaang You're right, they are guaranteed to be in the tree and for both of them to be different.
@mitramir5182
@mitramir5182 2 жыл бұрын
Thank you so much for the amazing explanation!
@crackfaang
@crackfaang 2 жыл бұрын
No problem, glad you enjoyed the video!
@PowerOfTens8420
@PowerOfTens8420 9 ай бұрын
That was a really great explanation! Thanks
@ВладСкригун
@ВладСкригун Жыл бұрын
big thanks for your video. good explanation. keep going.
@mdasifshahjalal3595
@mdasifshahjalal3595 2 жыл бұрын
Thanks for clearing this puzzle :)
@crackfaang
@crackfaang 2 жыл бұрын
No problem, glad you enjoyed the video
@shuier525
@shuier525 9 ай бұрын
You are a magic
@subee128
@subee128 10 ай бұрын
Thanks
@cicis3621
@cicis3621 10 ай бұрын
genius thank you
@khaledgewily8824
@khaledgewily8824 Жыл бұрын
Thank you :)
@hangchen
@hangchen Жыл бұрын
I am the 100th liker! Thank you!
@vedansh9004
@vedansh9004 4 ай бұрын
Goddamn
@lukaszm9234
@lukaszm9234 Ай бұрын
I hate this "interview mentality", where "it doesn't hurt" to check if something is null, even when we're guaranteed that it won't be null. If I saw that in a PR I'd immediately point it out. Useless code should be deleted, not kept "just in case". Companies will hire the "Leetcode specialists" and the codebase will suffer. Please do better.
LOWEST COMMON ANCESTOR OF A BINARY TREE II | PYTHON | LEETCODE 1644
20:46
Lowest Common Ancestor (LCA) Problem | Eulerian path method
17:02
WilliamFiset
Рет қаралды 45 М.
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 7 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 12 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
L27. Lowest Common Ancestor in Binary Tree | LCA | C++ | Java
14:09
take U forward
Рет қаралды 328 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 835 М.
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)
20:30
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Distribute Coins in Binary Tree - Leetcode 979 - Python
17:41
NeetCodeIO
Рет қаралды 17 М.
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 252 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 501 М.
Симбу закрыли дома?! 🔒 #симба #симбочка #арти
00:41
Симбочка Пимпочка
Рет қаралды 5 МЛН