LOWEST COMMON ANCESTOR OF A BINARY TREE II | PYTHON | LEETCODE 1644

  Рет қаралды 6,405

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 14
@symbol767
@symbol767 2 жыл бұрын
Within in the first 3 minutes of your video I figured out why my code wasn't working. Once I did a check for P and for Q separately first to confirm they both exist, my solution started working. All I had to do is check if P or Q doesn't exist and return Null, if they both exist, then I can do the normal "find ancestor" code. But of course this wasn't efficient enough, so thank you for showing me the optimized version
@chasing_the_horizon
@chasing_the_horizon 2 жыл бұрын
Wow, the explanation was super clear! Thx, mate!
@crackfaang
@crackfaang 2 жыл бұрын
Thanks! Make sure to subscribe if you haven’t already 😉
@FluteVJ
@FluteVJ Жыл бұрын
Thank you . Your channel videos are super easy to understand.
@Rutik9999
@Rutik9999 Ай бұрын
a bit complex, you can just find lca like (lca of BT 1) and get node1 and then perfrom node2 = lca(node1.left) or lca(node1.right) and if both nodes then return node1, else None
@jimmyahmed5424
@jimmyahmed5424 2 жыл бұрын
Thank you for explaining, it was helpful!
@crackfaang
@crackfaang 2 жыл бұрын
Glad you found the video helpful! Make sure to subscribe so you don’t miss other videos coming soon
@jaisoni1
@jaisoni1 2 жыл бұрын
Great Explanation! Thanks!
@crackfaang
@crackfaang 2 жыл бұрын
Glad you enjoyed the video! Make sure to subscribe so you don’t miss future videos
@hangchen
@hangchen Жыл бұрын
8:59 sorry I am confused here. If returning 4 or 5 doesn't matter, how would the algorithm make sure to return the correct answer, 5 but not 4?
@klppdc
@klppdc 6 ай бұрын
Yes. This is the same question I have. Also if P and Q both are found in left subtree, why do we have to still traverse right subtree? That does not seem necessary.
@jayjw1
@jayjw1 17 күн бұрын
The way that the statement 'return l or r' works is it will return whichever one of those has a value attached to it. So in the example in the video (covering the example where p = 5 and q = 10), it would return the node containing the value of 5.
@jayjw1
@jayjw1 17 күн бұрын
@@klppdc Because it is not guaranteed that both P and Q will always be found in the left subtree. An algorithm only containing the code to find that would not pass all of the test cases that this question is asking for. So we must make sure that we also cover cases where P and Q are in separate subtrees and where P or Q (or both) do not exist.
@davidc4322
@davidc4322 Жыл бұрын
Wouldn't storing l and r result in an O(N) space complexity? Since the None value also takes space in memory as well.
LOWEST COMMON ANCESTOR OF A BINARY TREE IV | PYTHON | LEETCODE 1676
10:51
LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236
12:48
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)
20:30
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 193 М.
Binary Search Tree Iterator - Leetcode 173 - Python
12:47
NeetCode
Рет қаралды 44 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 88 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 240 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 301 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН