Distribute Coins in Binary Tree | Distribute candies in a binary tree | Leetcode 979 | GFG

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 109

  • @subhamsaha3810
    @subhamsaha3810 10 месяцев назад +57

    For every Candidate who gave up on DSA you adds a new Story in their life.

  • @DevOpskagyaan
    @DevOpskagyaan 10 месяцев назад +16

    🔥🔥 Let’s make this guy famous.
    He deserves it

  • @DurgaShiva7574
    @DurgaShiva7574 10 месяцев назад +5

    ekdum se inhone waqt badal diya, jasbat badal diye, coding ke halat badal diye... GOAT for a reason

  • @prateekgirilateralentry5865
    @prateekgirilateralentry5865 10 месяцев назад +11

    The explanation of this problem is truly outstanding, leaving no room for confusion.
    thank you Sir 😀

  • @sv_patil45
    @sv_patil45 10 месяцев назад +9

    Story wale Bhaiyyaa🤩
    thank you. u are superb!!!!!

  • @souravjoshi2293
    @souravjoshi2293 10 месяцев назад +6

    Hats off to your unique style of teaching. Every point cleared.

  • @aparanabhatt
    @aparanabhatt 10 месяцев назад +4

    Finally I found a channel that ...explains concepts exactly the way I understand. Its really helpful.Thankyou for the initiative .Keep up the good work.

  • @Ankitkumar-fz3kc
    @Ankitkumar-fz3kc 10 месяцев назад +4

    Thanks sir for the best explaination please bring GFG POTD solution regularly just like LC POTD .

  • @theslimeoxidizer3308
    @theslimeoxidizer3308 6 месяцев назад +1

    thank you mazhar sir...i first attempted this question 7-8 months back and after not approaching and not understanding through different video tutorials I've decided to left this question and now I totally understood this question thank you...

  • @hengulrajsaikia9227
    @hengulrajsaikia9227 6 месяцев назад +8

    i think i might be one of your earliest students... since 2.5k subscribers...

  • @ankanbrahmachary6581
    @ankanbrahmachary6581 6 месяцев назад +1

    10 mins into your video I got the idea how to write the story .. thanks for making DSA easy as a cake ... couldn't have done without you

  • @sauravchandra10
    @sauravchandra10 10 месяцев назад +2

    Best explanation so far! You never disappoint us!

  • @nishantdehariya5769
    @nishantdehariya5769 Месяц назад +1

    awesome story leading to awesome algo

  • @dyashwanth9822
    @dyashwanth9822 6 месяцев назад +1

    Thanks Bhai 12:30 yaha tak dekha almost aagaya tha par ye example fail hora tha Input: root = [0,3,0] Output: 3 then "story to code" timestamp wala part dekha got an idea of if child is 0 then we have to send -1. Once again Thanks bhai ❤️❤️

  • @guddukumar-v8z5l
    @guddukumar-v8z5l 6 месяцев назад +1

    Thanks for teaching DSA in this story wise way.

  • @nityaagrawal1024
    @nityaagrawal1024 6 месяцев назад +1

    best solution for this question so far

  • @EB-ot8uu
    @EB-ot8uu 6 месяцев назад +1

    bhai hats off to yaar. kya hi amazing tarika hai aapke explain karne ka. just mind blowing

  • @savage_2431
    @savage_2431 6 месяцев назад +1

    Very nice explanation

  • @sakshisah3130
    @sakshisah3130 2 месяца назад

    Amazingggg explanation specially the negative move part

  • @shikharpandya4927
    @shikharpandya4927 2 месяца назад

    Did the question on my own
    Thanks a lot😇

  • @Rahul-pr1zr
    @Rahul-pr1zr 6 месяцев назад

    Amazing explanation, crystal clear!

  • @upcoming_Engineer_
    @upcoming_Engineer_ 6 месяцев назад +2

    thanks for this AMAZING story solution.

  • @Explore_with_anish
    @Explore_with_anish 6 месяцев назад +1

    you explanation is amazing. you have given me hope.

  • @AnjuGupta-sz1uk
    @AnjuGupta-sz1uk 10 месяцев назад +1

    loved the approach

  • @kartikforwork
    @kartikforwork 6 месяцев назад +1

    i write things just like you did, but was unsure how to add those up in moves to ans, i messed with code and i don't know why i wrote abs(left)+abs(right), but it got accepted. maybe my knowleged and thought process is increasing by doing potd and watching ur solutions

  • @Lakshya-f4l
    @Lakshya-f4l 4 месяца назад +1

    Thankyou bhaiya!

  • @Inspire_with_SM
    @Inspire_with_SM 6 месяцев назад +1

    waiting for 50k bhaiya

  • @Abhay14
    @Abhay14 10 месяцев назад +3

    bhaiya ek request h jaise aap phle hrr question ke phle motivation quote dette the 2 min ka vo fir se continue kr do pls

  • @gauravbanerjee2898
    @gauravbanerjee2898 6 месяцев назад +1

    Thanks a lot bhaiya ❤❤

  • @ayushtandon1719
    @ayushtandon1719 6 месяцев назад

    brilliant explanation!

  • @gauravbanerjee2898
    @gauravbanerjee2898 10 месяцев назад

    Was eagerly waiting for this video thanks a lot bhaiya ❣️

  • @naikajsevak6090
    @naikajsevak6090 10 месяцев назад +1

    thanks sir great explanation ☝

  • @AmanKumar-qz4jz
    @AmanKumar-qz4jz 10 месяцев назад +1

    wow bhaiya!!! impressive!!!

  • @anuppatankar4294
    @anuppatankar4294 10 месяцев назад

    Great video 👌🏻
    Please make a video on todays leetcode contest question 4.

  • @codeandtalk6
    @codeandtalk6 10 месяцев назад +1

    ❤❤❤❤❤❤waited

  • @prateekgirilateralentry5865
    @prateekgirilateralentry5865 6 месяцев назад +1

    Thank you sir ...

  • @suyashjain3223
    @suyashjain3223 6 месяцев назад +1

    Nice Explanation!!

  • @simarjeetkaur6781
    @simarjeetkaur6781 10 месяцев назад

    Great explaination ✨ Sir please upload solutions for biweekly contest if possible..

  • @AmarjeetKumar-to9ub
    @AmarjeetKumar-to9ub 10 месяцев назад +1

    Thank You Sir :)

  • @hvsmp1768
    @hvsmp1768 6 месяцев назад +2

    bhai kya bawal samjhate ho , maja aa jata h.

  • @codestorywithMIK
    @codestorywithMIK  6 месяцев назад +26

    Similar Leetcode Problem (POTD Today of 18th May, 2024) - leetcode.com/problems/distribute-coins-in-binary-tree

    • @priyanshkumar17
      @priyanshkumar17 6 месяцев назад +1

      Sir, in the problem statement posted by Leetcode in today's daily challenge demands to calculate the minimum number of moves. It took me around 30-40 minutes to understand the significance of the word "minimum". I was thinking about multisource BFS, but could not progress further.

    • @parvahuja7618
      @parvahuja7618 6 месяцев назад +1

      thankyou sir came here cause of the potd, but will be staying cause of your amazing narration.

  • @saxena11ashish
    @saxena11ashish 6 месяцев назад +2

    One day when I'll make it to google, I will not forget to mention you.

    • @codestorywithMIK
      @codestorywithMIK  6 месяцев назад +1

      This means a lot. I am sure you will reach your goal. ❤️

  • @wearevacationuncoverers
    @wearevacationuncoverers 10 месяцев назад

    Guruji. Itna acha koi kaise parha sakta hai bhai.

  • @thekindspill
    @thekindspill 10 месяцев назад

    You are unique.

  • @mayankupreti6966
    @mayankupreti6966 5 месяцев назад

    Very Nice explanation.Just one query ..where do you find so much variety of problems for a single topic, is there any list u following/or any forums?or random pick

  • @tarunsingh2480
    @tarunsingh2480 6 месяцев назад +1

    great explaination

  • @Ramneet04
    @Ramneet04 10 месяцев назад

    Hi sir, can you please make a video on questions to get started for competitive programming on Codeforces. Or guide for the same.

  • @rahuljha972
    @rahuljha972 4 месяца назад

    please make more and more videos on graphs

  • @umeshbisht1054
    @umeshbisht1054 10 месяцев назад +1

    Thanku bhaiya ❤

  • @Оксана-ъ7я6к
    @Оксана-ъ7я6к 6 месяцев назад +1

    can anybody please explain kinda a proof how this equation { moves += abs(l) + abs(r) } works not able to visualise that coins/candy movement how adding that extra on left and right subtree gives us total moves

  • @aws_handles
    @aws_handles 10 месяцев назад

    Hats off 🙏🏻

  • @vipulaggarwal8518
    @vipulaggarwal8518 6 месяцев назад +2

    bhai aapko sirf voice se hi pehchante hai apna face bhi reveal krdo kabhi😅😅😂

  • @nikeshsingh2081
    @nikeshsingh2081 10 месяцев назад +2

    Water the plants on gfg ,ka code jo apne karaye hai similar questions Leetcode 1326 par work nahi kar raha hai...why?

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад +1

      They are not entirety same. One difference you can see that in GFG, the element can also be 0
      But in leetcode all number will be > 0

    • @nikeshsingh2081
      @nikeshsingh2081 10 месяцев назад

      @@codestorywithMIK code me kis point par change karne hoga.
      Kay app code me change kare ke GitHub par push kar sakte hai (for Leetcode_1326)?

  • @lostcyrus8578
    @lostcyrus8578 10 месяцев назад

    today's gfg problem vertex cover ki bhi video upload krdo bhaiya

  • @MixedPlatter
    @MixedPlatter 6 месяцев назад +1

    Hi, I am not getting one thing here. In JAVA code (from your github links), I am able to see you have used one moves array, and adding value at it 0th index and returning 0th index. But for C# it seems moves is an integer variable. That makes sense! What I wanted to know is why in JAVA we used an array instead of moves as integer variable?(In that case I am getting error as well). Would be great, if you please explain!

    • @codestorywithMIK
      @codestorywithMIK  6 месяцев назад

      In Java, primitive data types like int are passed by value, meaning when you pass an int to a method, a copy of the value is made and passed to the method. Changes made to the parameter inside the method do not affect the original value outside the method.
      To achieve similar behavior to passing by reference in C++, you can use an array or an object. In the provided Java code:
      public static int distributeCandy(Node root) {
      if (root == null || (root.left == null && root.right == null)) {
      return 0;
      }
      int[] moves = {0};
      solve(root, moves);
      return moves[0];
      }
      Here, moves is an array of size 1. Arrays are objects in Java, and when you pass an array to a method, you are passing a reference to the array object. Inside the distributeCandy method, moves[0] is modified by the solve method, effectively mimicking the behavior of passing moves by reference.
      private static int solve(Node root, int[] moves) {
      if (root == null) {
      return 0;
      }
      int l = solve(root.left, moves);
      int r = solve(root.right, moves);
      int totalExtraCandies = (l + r + root.data) - 1;
      moves[0] += Math.abs(l) + Math.abs(r);
      return totalExtraCandies;
      }
      The solve method takes an array moves as a parameter. Inside the solve method, the value of moves[0] is updated to reflect the total moves made. Since arrays are objects in Java, changes made to the array inside the solve method will be reflected outside the method, similar to passing by reference in C++.

    • @MixedPlatter
      @MixedPlatter 6 месяцев назад

      @@codestorywithMIK Thanks a lot for this much brief!🙂
      Now it makes sense!

  • @muskangupta2144
    @muskangupta2144 7 месяцев назад

    Can you plz, make a vdo on this question from leetcode - 1993. Operations on Tree, It's a request

  • @nish0798
    @nish0798 6 месяцев назад

    bro ek baat batatyo ki recursion question mein hum question puchte hai ki jaise aap ne question mein pucha ki left and right se kitni extra candies ye kaise pata chalta hai ki hum recursion se kya question puche ki wo hamara sub problem solve karde

  • @ishaansharma6553
    @ishaansharma6553 10 месяцев назад +1

    i love you bhaiya 😭😭😭😭

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад +1

      Love you all 😇❤️🙏
      Keep working hard.
      We all will rock

  • @Artiflex31
    @Artiflex31 6 месяцев назад

    Bhaiya we want regular gfg potd also ❤

  • @ugcwithaddi
    @ugcwithaddi 10 месяцев назад

    👌🏻👌🏻

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 10 месяцев назад

    class Solution {
    int ans=0;
    public int distributeCandies (Node root){
    dfs(root);
    return ans;
    }
    public int dfs(Node root){
    if(root==null) return 0;
    int l=dfs(root.left);
    int r=dfs(root.right);
    ans+=Math.abs(l)+Math.abs(r);
    return root.data+l+r-1;
    }
    }
    🎉❤

  • @YASHBHARDWAJ-f9i
    @YASHBHARDWAJ-f9i 6 месяцев назад +2

    sir gfg pr aapka java wala solution error dera h please check...

    • @codestorywithMIK
      @codestorywithMIK  6 месяцев назад

      Updated the code now. It works fine now ❤️

  • @abhishekverma7604
    @abhishekverma7604 10 месяцев назад +2

    bhya please solve this question
    leetcode 1366 :
    Rank Team by Votes..
    I'm facing lot of problem in optimization of this problem.
    please have a look

  • @daniel-so7cv
    @daniel-so7cv 10 месяцев назад

    Mik sir enable topmate...

  • @gp7060
    @gp7060 8 месяцев назад

    Thank you 🥹

  • @Trendspra
    @Trendspra 10 месяцев назад +1

    🙇‍♂🙇‍♂🙇‍♂

  • @sahilgupta7001
    @sahilgupta7001 6 месяцев назад

    how did you arrive at the conclusion that moves should be l+r

  • @dhairyachauhan6622
    @dhairyachauhan6622 10 месяцев назад +1

    Hi bhaiya aaj 1 month baad contest diya. Solved 2 question but took 1.5hrs, earlier used to do in 30-40 min. Looks like exams had a big toll on me :( Kya karu , is it normal ??

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад +1

      Don’t worry. It happens.
      Keep giving contests no matter what.

  • @bhartendupant8859
    @bhartendupant8859 10 месяцев назад

  • @shabananoor9423
    @shabananoor9423 6 месяцев назад

  • @ratishjain2718
    @ratishjain2718 6 месяцев назад +1

    bhai tu goat hain yaar sab samaj aagaya fr

  • @kumkumslab5811
    @kumkumslab5811 10 месяцев назад +1

    You are O+

  • @ManojKrVerma-vw4dx
    @ManojKrVerma-vw4dx 6 месяцев назад

    Question kya puchna hai recursion mei, yhi issue hain 😢

  • @aizad786iqbal
    @aizad786iqbal 6 месяцев назад

    when to create a new function and when to just use the given one only ?

    • @codestorywithMIK
      @codestorywithMIK  6 месяцев назад +1

      Actually you can ALWAYS write a separate function of your own to call in recursion.
      What I always do is that whenever you have to pass an extra parameter which is not present in the main function (provided by leetcode), then I write my own function.
      If I don’t have to pass any extra parameter, then the given function can itself be used to call in recursion.

  • @sureshchelani7497
    @sureshchelani7497 6 месяцев назад

    sir agar coins bich wale node per hua toh
    matlab
    0
    4 0
    0
    phir kya hoga?

    • @gui-codes
      @gui-codes 6 месяцев назад +1

      Output will be 4.
      Left most leaf(0) will return -1 to it's parent
      Then node(4) k lie totalExtraCandies = (-1 + 0 + 4) - 1 = 2
      Now it will calculate moves = abs(l) + abs(r) = 1;
      return totalExtraCandies = 2
      Not node(0) i.e. root will get l = 2 from its left child as calculated above and similarly it will get r = -1 from it's right child.
      moves += abs(l) + abs(r)
      moves = 1 + (abs(l) + abs(r))
      moves = 1 + (2 + 1) = 4

  • @ankitgupta2745
    @ankitgupta2745 10 месяцев назад +1

    Sir Today GFG Problem??????

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад

      Unfortunately I couldn’t get time today.
      Will definitely try to upload tomorrow 🙏🙏

  • @ashutoshpandey1639
    @ashutoshpandey1639 10 месяцев назад

    suggest some more problems so that I can confident in making stories before coding the tree problem

    • @codestorywithMIK
      @codestorywithMIK  10 месяцев назад +2

      Sure.
      Try Maximum Width Of Binary Tree
      Binary Tree Maximum Path Sum

  • @25-cse-csmohitkumarmandal59
    @25-cse-csmohitkumarmandal59 10 месяцев назад

    Bhai thora ad kam kri yar..30 min. 8-10 ad .. concentration kharab ho rha h