Binary Lifting (Kth Ancestor of a Tree Node)

Sdílet
Vložit
  • čas přidán 29. 03. 2021
  • Tutorial on binary lifting (also called jump pointers). We find k-th ancestor of a node in O(log(N)). Problem link leetcode.com/problems/kth-anc...
    Final code github.com/Errichto/youtube/b...
    Coding live streams - / errichto
    FAQ - github.com/Errichto/youtube/w...
    Subscribe for more educational videos on algorithms, coding interviews and competitive programming.

Komentáře • 190

  • @deepeiton6112
    @deepeiton6112 Před 3 lety +103

    The perfect video to find before going to bed :)

    • @shubhmishra66
      @shubhmishra66 Před rokem +6

      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 Před rokem +4

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

  • @sharinganuser1539
    @sharinganuser1539 Před 3 lety +61

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

    • @Errichto
      @Errichto  Před 3 lety +86

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

    • @ani68
      @ani68 Před 3 lety +9

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

    • @deepeiton6112
      @deepeiton6112 Před 3 lety

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

    • @user-qm4jh2oj6f
      @user-qm4jh2oj6f Před 3 lety

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

    • @user-qm4jh2oj6f
      @user-qm4jh2oj6f Před 3 lety

      @@deepeiton6112 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

  • @ani68
    @ani68 Před 3 lety +35

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

  • @vineethkumar8132
    @vineethkumar8132 Před 3 lety +44

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

  • @ANKITVERMA-fl1zn
    @ANKITVERMA-fl1zn Před 3 lety +11

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

  • @Zaii_3
    @Zaii_3 Před 2 lety +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!

  • @saranshdabas4223
    @saranshdabas4223 Před 3 lety +1

    Always love your explanations, the best stuff out there.

  • @nighteagle6297
    @nighteagle6297 Před 2 lety +1

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

  • @shauryagautam5143
    @shauryagautam5143 Před 3 lety +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!!

  • @wew8820
    @wew8820 Před měsícem

    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

  • @niwanshumaheshwari4534
    @niwanshumaheshwari4534 Před 2 lety +1

    Simply amazing, Thanks Errichto.

  • @shotakhakhishvili8640
    @shotakhakhishvili8640 Před rokem +1

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

  • @bhaskar08
    @bhaskar08 Před 3 lety +12

    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

  • @nguyentrongvanviet
    @nguyentrongvanviet Před 2 lety +1

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

  • @trineshreddybs842
    @trineshreddybs842 Před 3 lety +2

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

  • @anonymous3419
    @anonymous3419 Před 3 lety +1

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

  • @GGxEpsilon
    @GGxEpsilon Před 3 lety +7

    Thanks for uploading this 😊

  • @aravind1772
    @aravind1772 Před 2 lety +1

    Extremely well presented, thanks

  • @srishtikdutta8946
    @srishtikdutta8946 Před 3 lety +3

    Great, please bring more such videos.

  • @sonnandhkalaka7452
    @sonnandhkalaka7452 Před 3 lety +2

    Thanks for the awesome explanation

  • @AmitSharma-yb9vc
    @AmitSharma-yb9vc Před 2 lety

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

  • @aayushverma4139
    @aayushverma4139 Před rokem +1

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

  • @kxb6098
    @kxb6098 Před 2 lety +1

    thank you very much for this amazing tutorial

  • @gitanshgarg3146
    @gitanshgarg3146 Před 3 lety +2

    Thanks for such good content 🙌

  • @bobjoe5524
    @bobjoe5524 Před 2 lety +1

    Beautiful explanation

  • @coefficient1359
    @coefficient1359 Před 3 lety +2

    Thank you Errichto

  • @Squirrelschaser
    @Squirrelschaser Před 3 lety

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

  • @jdragon8184
    @jdragon8184 Před 2 lety +1

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

  • @AmitShukla-pw5hg
    @AmitShukla-pw5hg Před 2 lety +1

    Excellent material for CP

  • @artistic__08
    @artistic__08 Před 6 měsíci

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

  • @codejutsu2720
    @codejutsu2720 Před 2 lety +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 Před rokem

      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!

  • @prathmesh646
    @prathmesh646 Před 3 lety +2

    Perfect, Made my day.

  • @stith_pragya
    @stith_pragya Před 5 měsíci +1

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

  • @gauravupreti9340
    @gauravupreti9340 Před 8 měsíci

    Thanks for the to the point explanation

  • @amartyasarkar8631
    @amartyasarkar8631 Před 3 lety +1

    Awesome explanation

  • @PrajjwalMehta
    @PrajjwalMehta Před 2 lety +1

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

  • @pwshen5046
    @pwshen5046 Před 8 měsíci

    thanks its really clear and useful!

  • @rishijha9860
    @rishijha9860 Před 3 lety +1

    Very nicely explained 🙂

  • @Dontpushyour_luck
    @Dontpushyour_luck Před 8 měsíci

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

  • @akanshkumar2876
    @akanshkumar2876 Před 3 lety +1

    Thanku Errichto🙃

  • @vishnusingh9590
    @vishnusingh9590 Před 3 lety +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

  • @tharunchowdary14
    @tharunchowdary14 Před 3 lety

    please do this kind of videos, Thank you!

  • @siddharthmagadum16
    @siddharthmagadum16 Před 2 lety

    Awesome explanation.

  • @user-pi2zd4xj8z
    @user-pi2zd4xj8z Před 2 měsíci

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

  • @itz_me_imraan02
    @itz_me_imraan02 Před rokem +3

    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..

  • @honeysingh1996
    @honeysingh1996 Před 2 lety

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

  • @paritoshsaurabh9851
    @paritoshsaurabh9851 Před 2 lety +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...

  • @mradultiwari5820
    @mradultiwari5820 Před 3 lety +1

    🙏🏻🙏🏻got a couple of new thoughts 💛

  • @madhavjha5289
    @madhavjha5289 Před rokem +2

    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

  • @thetester8371
    @thetester8371 Před 3 lety +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  Před 3 lety +20

      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 Před 2 lety +3

      @@Errichto I love you

  • @rahultrivedi1166
    @rahultrivedi1166 Před 2 lety +1

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

  • @teddq7210
    @teddq7210 Před 3 lety

    Happy Easter ❤️

  • @sandeepradhakrishnapadhi6366

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

  • @prateekgautam7398
    @prateekgautam7398 Před 3 lety +2

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

  • @sharadsharma3176
    @sharadsharma3176 Před 3 lety +10

    Waiting for game theory related video, sir.🥺

  • @eng.khalid9763
    @eng.khalid9763 Před 3 lety +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

  • @kabboghosh1853
    @kabboghosh1853 Před 2 lety +1

    best explanation of the world

  • @abhisheksharma7056
    @abhisheksharma7056 Před 2 lety +1

    thank you for this video;

  • @gena8414
    @gena8414 Před 3 lety +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).

  • @tritranminh2808
    @tritranminh2808 Před 3 lety +1

    Thank you so much.

  • @AnuragSingh7
    @AnuragSingh7 Před rokem +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 Před 10 měsíci

      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...

  • @praveenj3112
    @praveenj3112 Před 3 lety +10

    Superb solution. initially hard to understand

  • @ilikememes9052
    @ilikememes9052 Před 8 měsíci

    @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

  • @satyambhardwaj2289
    @satyambhardwaj2289 Před 3 lety +1

    appreciate it! Thanks

  • @anuragsekhri2315
    @anuragsekhri2315 Před 2 lety +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 Před 2 lety

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

  • @satyampal7235
    @satyampal7235 Před rokem +1

    Thanks a lot.

  • @RaoVenu
    @RaoVenu Před 3 lety +1

    @Errichto At 16:30, line 30, you use
    if (k & (1

  • @ruslankushnir761
    @ruslankushnir761 Před 3 lety +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  Před 3 lety +1

      Dark gray seems neutral enough, I think.

    • @ruslankushnir761
      @ruslankushnir761 Před 3 lety

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

  • @josephwong2832
    @josephwong2832 Před 3 lety +1

    nice job

  • @MrDivad006
    @MrDivad006 Před 3 lety +3

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

    • @Errichto
      @Errichto  Před 3 lety +3

      that's a lot of videos :|

    • @MrDivad006
      @MrDivad006 Před 3 lety

      @@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 Před 3 lety +2

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

  • @satishshingade7469
    @satishshingade7469 Před 2 lety +1

    Thanks !!

  • @anshprakash1778
    @anshprakash1778 Před 2 lety +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.

  • @vamsivishalallu1931
    @vamsivishalallu1931 Před 2 lety +2

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

  • @CryingMG
    @CryingMG Před 3 lety +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!

  • @vivekdubey2068
    @vivekdubey2068 Před 3 lety +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?

  • @CristhianR51
    @CristhianR51 Před 3 lety +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  Před 3 lety +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 Před 2 lety +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 Před 2 lety

      @@jahnvikumari318 Cheers for that!

    • @shoyaishida3605
      @shoyaishida3605 Před rokem

      @@jahnvikumari318 🥲

    • @Dontpushyour_luck
      @Dontpushyour_luck Před 8 měsíci

      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

  • @vikasgoyat3479
    @vikasgoyat3479 Před rokem +1

    nice video

  • @cipherbenchmarks
    @cipherbenchmarks Před 2 lety

    Thanks pls do graphs dfs and bfs.

  • @user-zl7xr5bo2r
    @user-zl7xr5bo2r Před 3 lety +1

    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)

  • @yaseengousesamudri9390
    @yaseengousesamudri9390 Před 3 lety +2

    Add this video to the Edu playlist please

  • @Aditya-rp1lr
    @Aditya-rp1lr Před 3 lety +1

    waiting for sparse table lecture

  • @VK-oq6cb
    @VK-oq6cb Před 3 lety +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  Před 3 lety

      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 Před 3 lety

      @@Errichto thanks a lot :-)

  • @shrutimahajan7901
    @shrutimahajan7901 Před 2 lety +1

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

  • @joseluisdeleon1479
    @joseluisdeleon1479 Před 3 měsíci

    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

  • @prateekjoshi5494
    @prateekjoshi5494 Před 2 lety +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!

  • @nj9362
    @nj9362 Před 3 lety

    What hardware/software do u use to teach and write?

  • @samyakjain5158
    @samyakjain5158 Před rokem +1

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

  • @debanshusekharjena280
    @debanshusekharjena280 Před 3 lety +2

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

    • @Errichto
      @Errichto  Před 3 lety +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.

  • @5590priyank
    @5590priyank Před 3 lety

    Can we use fenwick tree trick for jumping to parents for finding kth ancestor?
    currently, if k = 21, we are jumping like 1 then 4 then 16. How about we jump in reverse like 16 then 4 then 1?
    we can use following to move to 16 from 21:
    int nextJump = k & -k;
    then
    k -= nextJump; // this makes k = 5 again
    and we repeat same.

    • @Errichto
      @Errichto  Před 3 lety +1

      What you described is another way to get the binary representation of K. It's just commonly used in Fenwick trees.

  • @dst8676
    @dst8676 Před 3 lety +1

    what is software you used to present

  • @0chunhui
    @0chunhui Před 3 lety +1

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

  • @naveen3192
    @naveen3192 Před 3 lety

    What's the tool you use to draw man?

  • @govindtomar6956
    @govindtomar6956 Před 2 lety +2

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

  • @ritikraj26_
    @ritikraj26_ Před 9 měsíci

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

  • @yuvarajanm2059
    @yuvarajanm2059 Před 3 lety +1

    Bro I have a doubt , while debuging a program in vscode which uses array of string. I want to see all the elements while debuging, but it does not show like that. I need yr help bro. I stucked in that for 3 days..please if anyone knows help me.

  • @hardikagrawal3903
    @hardikagrawal3903 Před rokem

    when i did the question on leetcode par[i]

  • @naklecha
    @naklecha Před 3 lety

    Instead of depth can we check if node == up[node[j]] return -1

    • @Errichto
      @Errichto  Před 3 lety

      Are you trying to check if node is the root? I don't see how that helps.

    • @naklecha
      @naklecha Před 3 lety

      I meant instead of depth. We could go up the tree and if we get the condition we return -1.

  • @chaitanyavarma2097
    @chaitanyavarma2097 Před 2 lety

    Code is not working for some test cases now.

  • @virtyaluk
    @virtyaluk Před 2 lety +1

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

  • @maksadbek
    @maksadbek Před rokem +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 Před rokem

      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 Před rokem

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

    • @sudhanshukumarroy7018
      @sudhanshukumarroy7018 Před rokem

      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.