Paths from root with a specified sum | Simple | Story To Code | GfG POTD

Поділитися
Вставка
  • Опубліковано 4 січ 2025

КОМЕНТАРІ • 24

  • @GauravDuseja-t6q
    @GauravDuseja-t6q 11 місяців тому +1

    Bhaiya why not doing currSum-=temp.pop_back() may you please explain🙏

    • @codestorywithMIK
      @codestorywithMIK  11 місяців тому +7

      I believe you are asking why we are not doing
      curr_sum = curr_sum - temp.back()
      The reason is, we are not passing curr_sum as reference, so when the recursion rolls back, the curr_sum will come back to it last value at that recursion point.
      Notice that we are passing temp by reference, so any change to it will be reflected to every recursive point and hence we need to undo it before rolling back to last recursion point.
      Why we are passing temp by reference (&) ?
      Because, passing it by copy will increase time complexity because every time i pass a parameter, a copy will be made if I don’t pass it by reference.
      Hope this helps

    • @lostcyrus8578
      @lostcyrus8578 11 місяців тому

      @@codestorywithMIK I was also confused in this. Thanks for answering

    • @GauravDuseja-t6q
      @GauravDuseja-t6q 11 місяців тому

      @@codestorywithMIK thanku bhaiya 🙏

  • @gauravbanerjee2898
    @gauravbanerjee2898 11 місяців тому

    Thanks a lot bhaiya ❣🫡

  • @wearevacationuncoverers
    @wearevacationuncoverers 11 місяців тому +3

    I am able to do these problems now because of you. Thank you from the bottom of my heart. From me and my entire friend circle.

  • @gui-codes
    @gui-codes 7 місяців тому

    I think you are the creator of "story to code" concept. Never seen this unique style before

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 11 місяців тому

    Good explanation understood i have solved this same intuition thanks jai shree ram.

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

    I have one question if we check for sum==TotalSum before adding value in current sum then it is giving multiple values whyy???

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

      did you got the answer??? i have the same question

  • @jambajuice07
    @jambajuice07 11 місяців тому +2

    what if we had to find the paths from any node to any node with sum k

    • @codestorywithMIK
      @codestorywithMIK  11 місяців тому +5

      In that case, you will have to use the same approach we would do in an array to find any subarray having sum = k.
      You can use map.
      Find the code below :
      class Solution {
      public:
      map mp;
      void pathNums(TreeNode* root, int currSum, int sum, int &count) {
      if(!root)
      return;
      currSum += root->val;
      if(mp.count(currSum-sum)) {
      count += mp[currSum-sum];
      }
      mp[currSum]++;
      pathNums(root->left, currSum, sum, count);
      pathNums(root->right, currSum, sum, count);
      mp[currSum]--;
      }
      int pathSum(TreeNode* root, int sum) {
      if(!root)
      return 0;
      mp.clear();
      mp[0] = 1;
      int count = 0, currSum = 0;
      pathNums(root, currSum, sum, count);
      return count;
      }
      };

    • @jambajuice07
      @jambajuice07 11 місяців тому

      @@codestorywithMIK got it thanks !

  • @ishaansharma6553
    @ishaansharma6553 11 місяців тому

    thankyou bhaiya

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

    Just a gentle advise bhai
    If you could arrange the playlist videos in order of difficulty it would boost your views.
    Also like Raj sir has uploaded videos in order jisme har particular topic (i.e graphs.dp,linked list) ke playlist ka agla video pichle video se related ho so it would definitely help you boost your channel exponentially.
    Btw nice video bhai keep it up♥️

    • @codestorywithMIK
      @codestorywithMIK  11 місяців тому +2

      Hi there,
      Actually i follow this pattern in my Concepts Playlist.
      As of now I have 2 Concepts playlist
      1) DP Concepts & Qns
      2) Graph Concepts & Qns
      It’s in order. Every video is related to last video . Topic wise coverage. You can find their links in the Description
      I am currently working on concepts playlist of other topics too. Soon they are coming.
      Thank you so much for this precious suggestion and your kind words. ❤️❤️🙏🙏

  • @ranjeetKumar-ik8sf
    @ranjeetKumar-ik8sf 11 місяців тому +2

    Hi, please upload the recursion concept and questions I have been waiting for the last two please this is my humble request because I tried many recursion playlists I am still confused about recursion.

  • @aneesh_sayal
    @aneesh_sayal 11 місяців тому

    MIK, would you be able to make a video for Leetcode 322, Coin Change. Also, Please update the DP concepts playlist. Love your solutions. Thanks

  • @pratyushraghuvanshi1543
    @pratyushraghuvanshi1543 11 місяців тому

    Leetcode 1477. Find Two Non-overlapping Sub-arrays Each With Target Sum plase can you explaination of this problem..please i request you post a video on this problem sir..

  • @bhartendupant8859
    @bhartendupant8859 11 місяців тому

  • @dhruvbandi6633
    @dhruvbandi6633 11 місяців тому

    Partition Array Into Two Arrays to Minimize Sum Difference | Leetcode #2035
    please make a video on this problem plzz

  • @krishnan828
    @krishnan828 11 місяців тому

    Jai Shree Ram