Distribute Coins in Binary Tree - Leetcode 979 - Python

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

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

  • @tommyshelby6277
    @tommyshelby6277 5 месяцев назад +39

    bro sneaked in `ladoos` and thought we wouldn't notice😂?

  • @arihantsinghrana2173
    @arihantsinghrana2173 5 месяцев назад +18

    Thank you soo much for making these video and soo early as well!!!

  • @АлекСневар
    @АлекСневар 5 месяцев назад +5

    Actually, the second solution with one variable is much easier to understand imho.

  • @weens571
    @weens571 5 месяцев назад +4

    Just when I needed the video. Thanks. 😁

  • @aashishbathe
    @aashishbathe 5 месяцев назад +7

    LADDOOO AGAINNN. Just remembering that, I just had it yesterday and it was great!

  • @HimanshuSaini-ur6by
    @HimanshuSaini-ur6by 5 месяцев назад

    This was great..by 9 minute mark I got an idea and coded it out. it workedd

  • @nikolatesla399
    @nikolatesla399 8 дней назад

    class Solution:
    res = 0
    def distributeCoins(self, root: Optional[TreeNode]) -> int:
    self.helper(root)
    # Retrieve the result and reset `Solution.res` for future calls
    ans = Solution.res
    Solution.res = 0
    return ans
    def helper(self, root: Optional[TreeNode]) -> int:
    if not root:
    return 0
    # Calculate excess coins for left and right children
    left = self.helper(root.left)
    right = self.helper(root.right)
    # Total moves required to balance current node
    Solution.res += abs(left) + abs(right)
    # Return net balance of coins after balancing current node
    return left + right + root.val - 1

  • @pastori2672
    @pastori2672 5 месяцев назад +2

    i actually struggled so much on this one and i thought your gonna clown them for the difficulty again but i guess not

  • @impolitebigmac3044
    @impolitebigmac3044 5 месяцев назад +38

    I hate leetcode

  • @MP-ny3ep
    @MP-ny3ep 5 месяцев назад

    Great explanation as always. Thank you

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

    Tough ques. thanks neetcode

  • @chien-yuyeh9386
    @chien-yuyeh9386 5 месяцев назад

    Thanks for sharing 🎉

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

    Interesting question.

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

    best explanation

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

    Hey, How would be solve this to get minimum number of moves if we had more total coins than number of total nodes?
    total coins > total nodes
    (Extended problem)

  • @realisticlevel2553
    @realisticlevel2553 16 дней назад

    harddd to come up with this
    I came up with the intuition of the +1 and -1 for when passing up coins and "asking" for coins, and I was trying to balance it with the current level of the tree :(

  • @sivaramakrishnankn1721
    @sivaramakrishnankn1721 5 месяцев назад +3

    boondhi laddoo gang represent

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

    How can we conclude that this question doesnt require DP? At first glance, we have choices and we need minimum, making it a good candidate for DP

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

    2055. Plates Between Candles... Next Please..🙏

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

    public int[] dfs(TreeNode root){
    if(root == null) return new int[]{0,0}; //size, coins
    int[] left = dfs(root.left);
    int[] right = dfs(root.right);
    int[] curr = new int[]{left[0]+right[0]+1, left[1]+right[1]+root.val};
    res += Math.abs(curr[0] - curr[1]);
    return curr;
    }
    java code

  • @kevinwang8632
    @kevinwang8632 5 месяцев назад +1

    this one was hard

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

    extra = left_extra+right_extra+1-node.val # calculated it from the first solution equations
    extra = left_extra+right_extra-1+node.val # provided by neetcode
    both is working don't know how

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

    Double bfs?

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

    i love ladoos

  • @Masterasadumar
    @Masterasadumar 5 месяцев назад +2

    lol ladu