Binary Lifting (Kth Ancestor of a Tree Node)

Поділитися
Вставка
  • Опубліковано 21 лис 2024

КОМЕНТАРІ • 206

  • @micro_seekers
    @micro_seekers 3 роки тому +126

    The perfect video to find before going to bed :)

    • @shubhmishra66
      @shubhmishra66 2 роки тому +9

      Finally I meet someone who feels the same... Even before going to bed, I can understand this guy's explanations in a short time, what a legend!

    • @sanyamjha5796
      @sanyamjha5796 Рік тому +7

      Aah, that connection when you feel the exact same way as a complete stranger on UA-cam..... The Internet did break many walls

  • @ani68
    @ani68 3 роки тому +38

    I always tried with O(n) way....thanks for this O(logN) way.....nice explanation....👍👍

  • @vineethkumar8132
    @vineethkumar8132 3 роки тому +47

    I'm a beginner at CP but still was able to understand much of this, Thank you for such an amazing explanation Erichto ❤️

  • @sharinganuser1539
    @sharinganuser1539 3 роки тому +71

    Thank god...I'm trying to understand this for a week now...

    • @Errichto
      @Errichto  3 роки тому +90

      I hope it doesn't take another week with my video :|

    • @ani68
      @ani68 3 роки тому +9

      @@Errichto it will surely not....😊

    • @micro_seekers
      @micro_seekers 3 роки тому

      @@Errichto if I want to find minimum number of numbers(power of 2) that sum upto some k. Does this approach hold?

    • @ОлегОратовский-ъ9н
      @ОлегОратовский-ъ9н 3 роки тому

      @@micro_seekers Yes, this idea is used in LCA. If you want, you can ask something particular and I will answer you.

    • @ОлегОратовский-ъ9н
      @ОлегОратовский-ъ9н 3 роки тому

      @@micro_seekers As far as I understood, you asked, If it's true, that any number can be represented as the number of sums of two. The answer is Yes, you can find this powers of two greedy. Go from bigger powers of two to lower.
      Example: 49 = 32 + 16 + 1, or 90 = 64 + 16 + 8 + 2.
      Good way to do it in C++:
      vectorpowers;
      int num; cin >> num;
      for (int i = 30; i >= 0; i--){
      if(num - (1 = 0){
      num -= (1

  • @codejutsu2720
    @codejutsu2720 2 роки тому +7

    Thanks, just realized this was the hardest leetcode question in Tree segment, helped a lot...... One small addition in the last part is that we don't need to calculate depth if we just check that node is -1 in each iteration
    code -
    for i in range(self.log):
    if k & (1

    • @glaucomasi
      @glaucomasi 2 роки тому

      you are right, I also implemented it this way, but he said that he didn't want to add extra steps in the pre-calc to make this work and preferred the depth way!

  • @wew8820
    @wew8820 7 місяців тому

    this guy learned english, and computer science and how to explain things clearly and put it all on youtube for free so us dumb people can learn, what a legend

  • @ANKITVERMA-fl1zn
    @ANKITVERMA-fl1zn 3 роки тому +13

    Yet Again Another Life Saver Video Lecture, I will share this to my friends Thanks a lot!

  • @shotakhakhishvili8640
    @shotakhakhishvili8640 2 роки тому +1

    Incredibly chilly vibe, good explanation... overall great video, thanks!

  • @artistic__08
    @artistic__08 Рік тому

    All this time op was like - "This is so simple".
    Thanks for the explanation.

  • @adityarai8963
    @adityarai8963 6 місяців тому

    These videos are gold mines, an ultimate treasure. Loved it.

  • @PrajjwalMehta
    @PrajjwalMehta 3 роки тому +1

    They added the test case you were talking about nodes violating the condition parent[i]

  • @bhaskar08
    @bhaskar08 3 роки тому +13

    I like to add just another node (N+1) above the root and parent of (N+1) is itself. now at every query if the ancestor (N+1) return -1

  • @jdragon8184
    @jdragon8184 3 роки тому +1

    who is here after edu 110 div 2 , thanks a lot i solved E after watching this

  • @anonymous3419
    @anonymous3419 3 роки тому +1

    Thank you sir, watching your video reminds me a lot when i participated in Math Contest...

  • @thetester8371
    @thetester8371 3 роки тому +13

    Cool lecture man.
    I used to think there was some sort of relation between binary search and binary lifting. Well there indeed is a relation, but not exactly. I still kinda hate how some chinese contestants use the word "binary search" and "binary lifting" interchangeably, although........I sort of understand why, especially if you into a habit of writing binary search using for loops instead of while .

    • @Errichto
      @Errichto  3 роки тому +21

      There is a bigger similarity if you implement binary search by using powers of two. Say that you are searching in range [L, R].
      for(int i = 19; i >= 0; i--) { int mid = L + (1

    • @MGtvMusic
      @MGtvMusic 2 роки тому +3

      @@Errichto I love you

  • @vishnusingh9590
    @vishnusingh9590 3 роки тому +9

    @Errichto - At 11:21 : You say "And there's this condition about parents which is also quite convenient". I don't think that's the condition you mean. The condition at 11:21 is parent[ i ] < n, which is NOT the same as : parent[ i ] < i

  • @ilikememes9052
    @ilikememes9052 Рік тому

    @Errichto 15:38 you dont need to change any condition for log calculation, All you have to do is to change the iteration in loop from j=0;j

  • @trineshreddybs842
    @trineshreddybs842 3 роки тому +2

    Thank you errichto keep doing videos about CP_topics which are hard to understand.

  • @mohanthmane
    @mohanthmane 3 місяці тому

    Best explanation of binary lifting in the internet

  • @anuragsekhri2315
    @anuragsekhri2315 3 роки тому +5

    Thanks for the explanation really helped a lot ,
    Now As the condition is parent[i] < n , loops order will get changed and we can avoid depth track too
    int[][] up;
    int log = 17;

    public TreeAncestor(int n, int[] parent) {


    up = new int[n][log];
    //assuming 0 is the root node
    parent[0] = -1;


    //setting up first ancestor
    for( int i = 0 ; i < n ; i++)
    up[i][0] = parent[i];


    //setting up other ancestors
    for( int j = 1 ; j < log ; j++)
    {
    for( int v = 0 ; v < n ; v++)
    {
    if(up[v][j-1] == -1)
    up[v][j] = -1;
    else
    up[v][j] = up[ up[v][j-1] ][j-1];
    }
    }


    }

    public int getKthAncestor(int node, int k) {

    for(int i = 0 ; i < log ; i++)
    {
    if(((1

    • @shrutimahajan7901
      @shrutimahajan7901 3 роки тому

      Can you please explain to me why this code only solving cases where parent[i]

  • @stith_pragya
    @stith_pragya 11 місяців тому +1

    Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @nguyentrongvanviet
    @nguyentrongvanviet 2 роки тому +1

    thanks for all of this insane useful thing for CP
    hope you have more video in the future

  • @Zaii_3
    @Zaii_3 3 роки тому +5

    Thank you Errichto! I'm a new subscriber to the channel and you've really helped me a lot! Your explanations are far more better than my mentors in school. TYSM!

  • @madhavjha5289
    @madhavjha5289 2 роки тому +3

    I think leetcode has changed the question a bit now, now it is not necessaary that parent.value < node.value
    what I did to solve this is find the order in which nodes appear so like using bfs and then applied the same
    // code
    class TreeAncestor {
    public:
    vector bfs(vector& arr,int i){
    vector res, visited(arr.size(),0);
    queue q;
    q.push(i);
    while(q.empty() == false){
    int len = q.size();
    while(len--){
    int node = q.front();
    q.pop();
    visited[node] = 1;
    //add to res
    res.push_back(node);
    for(int j: arr[node]){
    if(visited[j]) continue;
    q.push(j);
    }
    }
    }
    return res;
    }
    int LOG = 20;
    vector dp;
    TreeAncestor(int n, vector& parent) {
    //make tree here
    vector arr(n);
    for(int i=1;i

    • @nivedkml
      @nivedkml 2 місяці тому

      Instead of all this, all you got to do is switch the loops.

  • @aayushverma4139
    @aayushverma4139 2 роки тому +1

    Nice explanation . Binary lifting doesn't seem tough anymore :)

  • @paritoshsaurabh9851
    @paritoshsaurabh9851 3 роки тому +1

    We would be grateful if you would ever make a proper series, or course for competitive programming..
    From medium to very advanced methods concepts to solve hard competitive programming problems...

  • @MrDivad006
    @MrDivad006 3 роки тому +3

    More leetcode problems pleasee. Just go crazy and solve all leetcode problems and put them into a playlist

    • @Errichto
      @Errichto  3 роки тому +3

      that's a lot of videos :|

    • @MrDivad006
      @MrDivad006 3 роки тому

      @@Errichto That's true. Normal mortals can't do it. But someone like you can just rush through most problems, you can use your strengths to the fullest and bring value to the world that others can't. Just read a problem, state your thoughts and approach, code, next problem.

    • @anshuman5554
      @anshuman5554 3 роки тому +2

      @@MrDivad006 nah, rather have him do lectures like these and you can try solving all the problems with the concepts he teaches

  • @Dontpushyour_luck
    @Dontpushyour_luck Рік тому

    nice explanation. as a beginner, i was able to understand easily

  • @skr3601
    @skr3601 19 днів тому

    prefect video to show the concept of that, thank you so much!

  • @AnuragSingh7
    @AnuragSingh7 2 роки тому +4

    For case where we need to return "-1" : How about for every query we can calculate both Kth and (K-1)th ancestor. If they are equal we return -1 else we return Kth ancestor

    • @jonathanarthuritoaldisinag573
      @jonathanarthuritoaldisinag573 Рік тому

      I think you can't do that if k == 1. if k == 1, then we're gonna check the ancestor with distance 0 and 1. errichto's implementation itself i think it is unsuited to find the ancestor with distance 0. but if you make a if else statement, i think you can...

  • @nighteagle6297
    @nighteagle6297 3 роки тому +1

    Very thanks for the clear explanation. I understood it again very well. Thanks a lot, Errichto!

  • @praveenj3112
    @praveenj3112 3 роки тому +10

    Superb solution. initially hard to understand

  • @shauryagautam5143
    @shauryagautam5143 3 роки тому +1

    Thanks Errichto, just cz you have made the video, I gt to know a new concept I never heard about! Keep making such videos!! Helps a lot!!

  • @srishtikdutta8946
    @srishtikdutta8946 3 роки тому +3

    Great, please bring more such videos.

  • @UltraKillpc
    @UltraKillpc 9 місяців тому

    Just awesome, i hv gone through whole lot of comments, and the coders here are awesome.. great place to Meetup.

  • @rahultrivedi1166
    @rahultrivedi1166 3 роки тому +1

    powers of 2 technique is also used in BIT(Binary Indexed Tree)

  • @ruslankushnir761
    @ruslankushnir761 3 роки тому +1

    Hi Errichto, thank you very much for your tutorials, please consider possibility to make background of your videos little less black, for example blue dark or silver dark to make them more neural, I think that in that case watching your videos will be less stressful for the eyes, thanks

    • @Errichto
      @Errichto  3 роки тому +1

      Dark gray seems neutral enough, I think.

    • @ruslankushnir761
      @ruslankushnir761 3 роки тому

      @@Errichto ohhh, I watched several yours videos in parallel, black background is present on your first tutorials. Good luck in Code Jam competition

  • @sharadsharma3176
    @sharadsharma3176 3 роки тому +10

    Waiting for game theory related video, sir.🥺

  • @eng.khalid9763
    @eng.khalid9763 3 роки тому +1

    Thank you..
    could you please make a video on how to code branch and bound with the example on travelling salesman problem, knapsack problem or any other example.
    I want to know how to find the next children of the parent and how to get the final solution

  • @sandeepradhakrishnapadhi6366
    @sandeepradhakrishnapadhi6366 3 роки тому +1

    Thankyou..Sir,can you please make stream specifically related to mathematical background..

  • @prateekgautam7398
    @prateekgautam7398 3 роки тому +2

    Finally he remembered that he has a youtube channel. Thanks a ton.

  • @AmitShukla-pw5hg
    @AmitShukla-pw5hg 3 роки тому +1

    Excellent material for CP

  • @CristhianR51
    @CristhianR51 3 роки тому +14

    I understand that if "parent[i] < i", then you can safely compute the parent array for each V in increasing order.
    However on leetcode it states that "parent[i] < n", which is a different thing, no?
    Thanks for the video!

    • @Errichto
      @Errichto  3 роки тому +24

      Oh, good catch. I've misread the condition as "parent[i] < i". Then my solution shouldn't work with the current order of for-loops and the test data is apparently weak.

    • @jahnvikumari318
      @jahnvikumari318 3 роки тому +14

      @@Errichto Hey, thanks I have contributed a test case to Leetcode so that this fails. Sorry but now your code also fails.

    • @MGtvMusic
      @MGtvMusic 2 роки тому

      @@jahnvikumari318 Cheers for that!

    • @shoyaishida3605
      @shoyaishida3605 2 роки тому

      @@jahnvikumari318 🥲

    • @Dontpushyour_luck
      @Dontpushyour_luck Рік тому

      you are right. I used a visited array and called another function to compute for a node if it is not visited yet. i was confused on how his code passed the test cases. now i understand that test cases were weak in the past

  • @niwanshumaheshwari4534
    @niwanshumaheshwari4534 3 роки тому +1

    Simply amazing, Thanks Errichto.

  • @bobjoe5524
    @bobjoe5524 2 роки тому +1

    Beautiful explanation

  • @gena8414
    @gena8414 3 роки тому +2

    Video request: string algorithms (KMP, Manacher). They don't occur often on CF but do on ICPC and I would like a nice resource (there aren't any nice video resources on these topics).

  • @saranshdabas4223
    @saranshdabas4223 3 роки тому +1

    Always love your explanations, the best stuff out there.

  • @kabboghosh1853
    @kabboghosh1853 3 роки тому +1

    best explanation of the world

  • @itz_me_imraan02
    @itz_me_imraan02 2 роки тому +4

    The same code is giving WA in test case [4,[-1,2,3,0]], [2,3], [2,2] , [2,1]
    U considered the nodes to be in increasing order from top to bottom, dats why it failed when the nodes are in decreasing order..

  • @sonnandhkalaka7452
    @sonnandhkalaka7452 3 роки тому +2

    Thanks for the awesome explanation

  • @kxb6098
    @kxb6098 3 роки тому +1

    thank you very much for this amazing tutorial

  • @ironman7-eg1bk
    @ironman7-eg1bk 4 місяці тому +1

    i have a doubt can any one say that in the leetcode question they did not mention parent[i]

  • @gauravupreti9340
    @gauravupreti9340 Рік тому

    Thanks for the to the point explanation

  • @AmitSharma-yb9vc
    @AmitSharma-yb9vc 3 роки тому

    Now, It's looking so easy. Thank you.

  • @GGxEpsilon
    @GGxEpsilon 3 роки тому +7

    Thanks for uploading this 😊

  • @tharunchowdary14
    @tharunchowdary14 3 роки тому

    please do this kind of videos, Thank you!

  • @anshprakash1778
    @anshprakash1778 2 роки тому +5

    Thanks for the wonderful lecture.
    But I am a bit confused about the way depth[v] and up array have been computed in the leetcode problem, it is never mentioned that parent[v] < v. If this condition is not true then result should be incorrect. Right? Or I am missing something.

  • @coefficient1359
    @coefficient1359 3 роки тому +2

    Thank you Errichto

  • @mradultiwari5820
    @mradultiwari5820 3 роки тому +1

    🙏🏻🙏🏻got a couple of new thoughts 💛

  • @abhisheksharma7056
    @abhisheksharma7056 3 роки тому +1

    thank you for this video;

  • @vamsivishalallu1931
    @vamsivishalallu1931 3 роки тому +2

    How depth[v]=depth[parent[v]]+1????
    This holds if depth[parent[v]] is already computed which is not always true.

  • @govindtomar6956
    @govindtomar6956 3 роки тому +3

    Errichto your code isn't working for this problem, you have to use the dfs method to do preprocessing.

  • @madhukiranattivilli2321
    @madhukiranattivilli2321 3 роки тому +1

    Hi...
    from ur getKthAncestor() function :
    for (int j = LOG - 1; j >= 0; j--) {}
    if (k >= (1 = 2, k=3-2=1
    j=0, 1 >= 1, k=1-1=0
    STOP
    2 steps are redundant above.
    Instead, why don't we write mylog(x) function, which returns floor(log2(x)), and do this :
    while (k > 0) {
    j = mylog(k);
    node = up[node][j];
    k -= 1 0, j = 4, k = 19-16=3
    3 > 0, j = 1, k = 3-2=1
    1 > 0, j = 0, k = 1-1=0
    0 > 0 STOP
    - Madhukiran

  • @CryingMG
    @CryingMG 3 роки тому +2

    Superb video Errichto, would you be interested in making some String algorithms videos? Suffix array in NlogN, Ukkonen, Manacher, Hashing(This is going to be super informative, if you include the anti-hashing test breaks that were mentioned on codeforces a few years ago when you use overflow), Suffix Trie and use cases, Compressed Trie?
    This will add so much more value to your already amazing channel!:) Much thanks!

  • @amartyasarkar8631
    @amartyasarkar8631 3 роки тому +1

    Awesome explanation

  • @satyampal7235
    @satyampal7235 2 роки тому +1

    Thanks a lot.

  • @aravind1772
    @aravind1772 3 роки тому +1

    Extremely well presented, thanks

  • @yaseengousesamudri9390
    @yaseengousesamudri9390 3 роки тому +2

    Add this video to the Edu playlist please

  • @Squirrelschaser
    @Squirrelschaser 3 роки тому

    yo this vid came in clutch been trying to understnad this problem.

  • @teddq7210
    @teddq7210 3 роки тому

    Happy Easter ❤️

  • @honeysingh1996
    @honeysingh1996 3 роки тому

    What a video man..... Lovely... I really enjoyed and had fun watching it...... #LoveCoding

  • @yashsonawane5341
    @yashsonawane5341 Рік тому +1

    For those who are wondering why this solution is getting WA now on leetcode.....
    Leetcode has removed the condition (parent[i] < i)...
    You can change your code accordingly.

  • @Aditya-rp1lr
    @Aditya-rp1lr 3 роки тому +1

    waiting for sparse table lecture

  • @debanshusekharjena280
    @debanshusekharjena280 3 роки тому +2

    2:38 I think segment tree is divide and conquer approach ??

    • @Errichto
      @Errichto  3 роки тому +1

      Yeah, in some way it is. What I meant is that segment trees and fenwick trees are often implemented for perfect powers of two.

  • @gitanshgarg3146
    @gitanshgarg3146 3 роки тому +2

    Thanks for such good content 🙌

  • @helperbot5445
    @helperbot5445 2 роки тому +2

    Very Nice Explanation 😍.
    I think you calculated depth in wrong manner
    consider this case:
    ["TreeAncestor","getKthAncestor"]
    [[6,[-1,2,3,4,5,0]],[1,4]]
    depth array according to code will be [0,1,1,1,1,1] but it should be [0,5,4,3,2,1]
    and for node = 1, 4th ancestor should be 5 but it will give -1 as depth is wrong.

  • @rishijha9860
    @rishijha9860 3 роки тому +1

    Very nicely explained 🙂

  • @shrutimahajan7901
    @shrutimahajan7901 3 роки тому +1

    Can you please explain to me why this code only solving cases where parent[i]

  • @prathmesh646
    @prathmesh646 3 роки тому +2

    Perfect, Made my day.

  • @vishwasgupta6149
    @vishwasgupta6149 Рік тому +1

    this code no longer works since leet code improved its test cases.. actually the problem is depth is being wrongly calculated

  • @pwshen5046
    @pwshen5046 Рік тому

    thanks its really clear and useful!

  • @0chunhui
    @0chunhui 3 роки тому +1

    why could you use node that has not been calculated? I mean this part: for(int v=0; v

  • @tritranminh2808
    @tritranminh2808 3 роки тому +1

    Thank you so much.

  • @satyambhardwaj2289
    @satyambhardwaj2289 3 роки тому +1

    appreciate it! Thanks

  • @akanshkumar2876
    @akanshkumar2876 3 роки тому +1

    Thanku Errichto🙃

  • @vivekdubey2068
    @vivekdubey2068 3 роки тому +2

    When we exchange the for loops, (where the LOG for loop is before) will work in both cases (independent of the value/order of nodes) while calculating the up vector?

  • @ВиталийКаменский-я2ю

    Sir, May I have any advice on How and when to turn my kids to Math from your perspective ? They are 2yo, and I’m preparing now)

  • @cipherbenchmarks
    @cipherbenchmarks 2 роки тому

    Thanks pls do graphs dfs and bfs.

  • @josephwong2832
    @josephwong2832 3 роки тому +1

    nice job

  • @ritikraj26_
    @ritikraj26_ Рік тому

    The condition of the leetcode problem has changed. Now you will need to swap the loops. XD

  • @VK-oq6cb
    @VK-oq6cb 3 роки тому +2

    Hey Errichto , What can we do
    ..if we have two types of quries in which one query type is we can add or delete leaf node in tree and other type return Kth ancestor !?

    • @Errichto
      @Errichto  3 роки тому

      Easy: just compute all log(N) needed ancestors when adding a new leaf. No need to do anything when removing a leaf.

    • @VK-oq6cb
      @VK-oq6cb 3 роки тому

      @@Errichto thanks a lot :-)

  • @vivekdubey2068
    @vivekdubey2068 3 роки тому +1

    Liked the part where he gets AC but he is still busy explaining with the "same expression".

  • @samyakjain5158
    @samyakjain5158 2 роки тому +1

    Is the github solution fine? it gives some error on one of the leetcode estcases

  • @virtyaluk
    @virtyaluk 3 роки тому +1

    Though the provided solution does look correct, it doesn't pass 5 out of 15 test cases on leetcode :(

  • @prateekjoshi5494
    @prateekjoshi5494 2 роки тому +1

    Hi, you mentioned since input n can be upto 50k, quadratic runtime won't work. What is the limit of input till which quadratic would work and why? Thanks!

  • @dst8676
    @dst8676 3 роки тому +1

    what is software you used to present

  • @satishshingade7469
    @satishshingade7469 3 роки тому +1

    Thanks !!

  • @maksadbek
    @maksadbek 2 роки тому +1

    Why are you using log(n) instead of log(height of tree) in your implementation ? The n (number of nodes) must be larger than height.

    • @sudhanshukumarroy7018
      @sudhanshukumarroy7018 Рік тому

      Log2(n)+1 will provide the height of tree only, you can even use this condition to get you log value and it will work.

    • @maksadbek
      @maksadbek Рік тому

      @@sudhanshukumarroy7018 Did I understand correctly that n is a height of the tree in a worst case scenario ?

    • @sudhanshukumarroy7018
      @sudhanshukumarroy7018 Рік тому

      That's why I suppose for worst case tree we will get higher order ancestors rather than getting -1 too quickly in a binary tree like tree.
      It is supposed like log value is the value whose 2^LogValue will provide highest possible ancestor that can exist.

  • @joseluisdeleon1479
    @joseluisdeleon1479 9 місяців тому

    I´m trying to solve a similar problem but I need to calculate the sum of a given node and a number of parents ex: node 2 and 5 parents and l have to return the sum of the values of that nodes. Any ideas of how can calculate the sum if i have the table with the k ancestros

  • @vikasgoyat3479
    @vikasgoyat3479 2 роки тому +1

    nice video