LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]

  Рет қаралды 8,252

Cracking FAANG

Cracking FAANG

2 жыл бұрын

In this video we'll be solving a popular Facebook interview question: Lowest Common Ancestor of a Binary Tree III. This problem has two solutions, one being quite easy to derive and the other is one of those "ah-ha" trick solutions that you need to have seen before to know off the bat. But once you've seen it, the solution is so elegant that it will blow your mind and is the most optimal way to solve this particular question.

Пікірлер: 38
@veluchamy-zt7gd
@veluchamy-zt7gd 3 ай бұрын
Man, honestly it is one of the underrated channel. Your explanation is crystal clear, easy to understand. I had difficulty in understanding complex problem's solutions, but your videos really helped me. Thanks a lot :)
@hemmy123
@hemmy123 Ай бұрын
The two pointer approach took me a while to understand but I think this rephrasing might help a few people. As mentioned in the video, P_copy takes 3 steps and Q_copy takes 2 steps. This results in a difference of 1 step which is not what we want. What we need to do is essentially make them do the same amount of work so we arrive at the same node. What we can do is combine them, so P_copy and Q_copy do the same amount of work So if we do the 'reset' as mentioned in the video: P_Copy does 3 + 2 steps and Q_Copy does 2 + 3 steps. This results in the difference being 0 so no matter where they are in the tree they will always end up at the same place!
@eddiej204
@eddiej204 Жыл бұрын
The solution sounds like fast slow pointers technique and it is brilliant. Thanks a lot, ser.
@crackfaang
@crackfaang Жыл бұрын
Yea it’s super cool. Though a bit hard to come up with if you haven’t seen it before. My mind was blown when I saw the solution for the first time
@ebenezeracquah478
@ebenezeracquah478 Жыл бұрын
Highly appreciated! Bind blowing solution
@cheekyjay7800
@cheekyjay7800 2 жыл бұрын
Brilliant explanation! I got quite confused when I was browsing through the discussion sessions and saw these 5 lines of magic Lol... but your video just made it so clear!
@crackfaang
@crackfaang 2 жыл бұрын
Thanks for the kind words and glad you found value from the video explanation. Make sure to subscribe so you don’t miss future videos!
@MrDoubleWhy
@MrDoubleWhy 4 ай бұрын
Amazing explanation! Thank you!
@sunwoodad
@sunwoodad 3 күн бұрын
You said the time complexity of naive solution is O(2N), but I just think it's O(N). Even the traverse is twice which is by p and also by q, total sum of them won't be greater than N.
@bhaveshvarma4313
@bhaveshvarma4313 2 жыл бұрын
Thanks man, Now Understand similar question of finding intersection of two linked list.
@zhengyuzhou2288
@zhengyuzhou2288 3 ай бұрын
Thank you, that explanation is very clear.
@rsKayiira
@rsKayiira Жыл бұрын
This is such an excellent solution. I had to watch it twice though because it wasn't clear you would be swapping the pointers so I was wondering but once I figured that out I was like wow. One other thing is maybe calling them q_ptr and p_ptr would have been better and its odd that the examples require us to return values yet we return nodes
@markvaldez8602
@markvaldez8602 20 сағат бұрын
Awesome! Thank you!
@JRivero
@JRivero 14 күн бұрын
So clear. Thank you very much
@Mo.Abufouda
@Mo.Abufouda 2 жыл бұрын
This channel is underrated! Great work. Keep the good work! :)
@crackfaang
@crackfaang 2 жыл бұрын
Thank you for the kind words! I'll keep uploading, make sure you're subscribed so you don't miss future uploads :)
@louie0187
@louie0187 4 ай бұрын
Basically same "ah-ha" trick as Leetcode 160 -- Intersection of Two Linked Lists
@crackfaang
@crackfaang 4 ай бұрын
Yup!
@usrenaem
@usrenaem 2 ай бұрын
Everything is amazing in this video! With your explanation the second solution clicks pretty easily. But what's IQ one should have to be able to solve it at an interview, within 15-20 minutes and without seeing the solution before, it's probably around 160.
@crackfaang
@crackfaang 2 ай бұрын
You don't need a high IQ lol. You just need to have seen the question before and basically memorized the solution. Just the nature of the game unfortunately
@sarayarmohammadi3376
@sarayarmohammadi3376 20 күн бұрын
Thank you
@MinhNguyen-lz1pg
@MinhNguyen-lz1pg 2 жыл бұрын
Well done mate! Appreciate the thoughtful explanation!
@crackfaang
@crackfaang 2 жыл бұрын
Thanks and make sure to subscribe!
@shraddhabarke1750
@shraddhabarke1750 4 ай бұрын
amazing work!! :)
@dhanashreegodase7933
@dhanashreegodase7933 2 жыл бұрын
thanks
@preityhansaa9150
@preityhansaa9150 2 жыл бұрын
question: if a node is LCA of itself (descendant) meaning it is the parent of itself so do we have to add the same node as parent of itself instead of Null
@fallencheeto4762
@fallencheeto4762 4 ай бұрын
No way someone figures this out, but it makes so much sense lol
@bittu007ize
@bittu007ize 2 жыл бұрын
Love from India
@crackfaang
@crackfaang 2 жыл бұрын
Thank you for your support! Make sure to subscribe so you don’t miss future videos
@adiesha_
@adiesha_ 4 ай бұрын
I am hard time understand why the last solution is linear time. Suppose we have a tree with one left branch of length n-1 and one right branch of length n. Then in order to level the paths you need to run the loop n times, and path length is n. Shouldn't this be O(n^2)?
@Krankschwester
@Krankschwester 2 ай бұрын
The loop wouldn't run N times, but abs(depth of p - depth of q). I do agree that this solution doesn't seem very good. Imagine you have the same example you mentioned but one node is at depth 1e4, the other one at 1e6. You would need to repeat this loop abs(1e4 - 1e6) times, which is awful.
@preityhansaa9150
@preityhansaa9150 2 жыл бұрын
This is good! Thanks for the post. can you post a variation of this for other 2 LCA problems the other ones doesnt have reference to its parent 1644 1676
@crackfaang
@crackfaang 2 жыл бұрын
No problem my friend. If you subscribe I will make these two videos, just for you ;)
@preityhansaa9150
@preityhansaa9150 2 жыл бұрын
@@crackfaang subscribed!
@tempuser3560
@tempuser3560 5 ай бұрын
At 3:39, shouldn't the time complexity be 2*log(n) instead of 2N? We're only looking at the height of the tree in each case
@adiesha_
@adiesha_ 4 ай бұрын
It's not a balanced tree, as far as I understand
@leetsqlcode1906
@leetsqlcode1906 Ай бұрын
is it |b-a| = |a-b|?
@n_x1891
@n_x1891 6 ай бұрын
Tortoise hare strikes again
LOWEST COMMON ANCESTOR OF A BINARY TREE II | PYTHON | LEETCODE 1644
20:46
DOT PRODUCT OF TWO SPARSE VECTORS - 3 SOLUTIONS EXPLAINED [PYTHON]
31:06
Пробую самое сладкое вещество во Вселенной
00:41
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 99 МЛН
Was ist im Eis versteckt? 🧊 Coole Winter-Gadgets von Amazon
00:37
SMOL German
Рет қаралды 34 МЛН
LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236
12:48
Lowest Common Ancestor Binary Tree
11:08
Tushar Roy - Coding Made Simple
Рет қаралды 250 М.
ALL NODES DISTANCE K IN BINARY TREE | PYTHON | LEETCODE # 863
15:41
Cracking FAANG
Рет қаралды 6 М.
NEXT PERMUTATION | LEETCODE # 31 | PYTHON OPTIMAL SOLUTION
18:50
Cracking FAANG
Рет қаралды 7 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 613 М.
EXCLUSIVE TIME OF FUNCTIONS | LEETCODE 636 | PYTHON SOLUTION
15:42
Cracking FAANG
Рет қаралды 3,8 М.
Minimax: How Computers Play Games
14:37
Spanning Tree
Рет қаралды 197 М.
BASIC CALCULATOR II | LEETCODE 227 | PYTHON SOLUTION
15:40
Cracking FAANG
Рет қаралды 16 М.
Пробую самое сладкое вещество во Вселенной
00:41