131 - Swap Nodes[Algo] | Trees | Hackerrank Solution | Python

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

КОМЕНТАРІ • 28

  • @rahul_bali
    @rahul_bali 3 роки тому +6

    This is far better explanation than any other on UA-cam of this question.

  • @symbol767
    @symbol767 2 роки тому +12

    Whoever wrote this problem description needs to be fired immediately

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

    i pretty much had the same solution,
    1. create tree
    2. swap
    3. perform inorder traversal
    i didnt know that python recursion limit needed to be changed, i was pulling my hair out because the 2 of 12 test cases kept failing

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

    Aswin you are the goat! I have been watching a ton of your videos recently and you give great explanations. Thank you!

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

      Thanks for your kind words!!! I will try my best to post more videos!!!

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

    Great explanation...you made it simple...

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

    Hi Aswin! Thank you for all these hackerrank videos!
    Could you please explain why level % k == 0 instead of level == k.

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

      thanks!! There will be multiple levels in the tree that's why we didn't use equals as it only changes one level

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

      we must swap every level that is a multiple of k. ex: if k = 2, we must swap levels 2, 4, 6 etc. so by using level%k==0 we are swapping every level that is a multiple of k

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

    Thnak youu

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

    Hi OP, I seen quite a bit of approaches where people would just all together change the intention of this format of input, by just making it into a familiar structure of actual tree data structure. But, since the underlying knowledge point is about abstract inorder tree traversal. Would it be feasible to do it using the mathematical form of tree traversal? (meaning not physically change the input structure, and just jump from one index to the next via the 2^k difference from each depth of tree.) Would you have an approach like that?

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

      It would be possible, need to think about that and try it for some base cases

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

      @@HackersRealm if you get it working, please let me know! I only got it working for the test case 0 as it's restrained somehow by my swap iterations so I never get to traverse the entire tree.

  • @subhankargoswami3521
    @subhankargoswami3521 3 роки тому +4

    This problem is very difficult how can solve that so easily bro please tell me

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

      When you solve problems, you can use some of the logic for new problems, that's how i did it!!!

  • @md.masiud3495
    @md.masiud3495 3 роки тому

    it is very difficult to crack this with logic how u build that please tell the wa.i have to see solution to almost every problem ..it is very sad..

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

      Each person think in different way. I guess by exploring these techniques you will get your own way of logic

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

    all test cases are still not clear

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

    I don't understand why we do level%k? And why is level set to 1 for all queries? 1%k is weird.

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

      When we reach that level, we swap the nodes, or else we move to the next level, that's the logic

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

      @@HackersRealm all children of k are stored at k*number, that's why we do modulo, Right?

    • @Vivek-hf7mx
      @Vivek-hf7mx 3 роки тому

      @@ajitagupta746 level is initailly set to 1 because the root node is located at level 1 and every time we have to start from root node. But I don't get why do we need the modulo?

    • @Vivek-hf7mx
      @Vivek-hf7mx 3 роки тому

      Like if we want to swap nodes at level 10, then according to the algorithm why would we swap nodes at level 2, since we encounter level 2 while traversing?