Distribute Coins in Binary Tree - Leetcode 979 - Python

  Рет қаралды 17,677

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер
@arihantsinghrana2173
@arihantsinghrana2173 7 ай бұрын
Thank you soo much for making these video and soo early as well!!!
@tommyshelby6277
@tommyshelby6277 7 ай бұрын
bro sneaked in `ladoos` and thought we wouldn't notice😂?
@weens571
@weens571 7 ай бұрын
Just when I needed the video. Thanks. 😁
@HimanshuSaini-ur6by
@HimanshuSaini-ur6by 7 ай бұрын
This was great..by 9 minute mark I got an idea and coded it out. it workedd
@АлекСневар
@АлекСневар 7 ай бұрын
Actually, the second solution with one variable is much easier to understand imho.
@aashishbathe
@aashishbathe 7 ай бұрын
LADDOOO AGAINNN. Just remembering that, I just had it yesterday and it was great!
@shashwatsingh9247
@shashwatsingh9247 7 ай бұрын
Tough ques. thanks neetcode
@MP-ny3ep
@MP-ny3ep 7 ай бұрын
Great explanation as always. Thank you
@kwakukusi4094
@kwakukusi4094 7 ай бұрын
best explanation
@chien-yuyeh9386
@chien-yuyeh9386 7 ай бұрын
Thanks for sharing 🎉
@pastori2672
@pastori2672 7 ай бұрын
i actually struggled so much on this one and i thought your gonna clown them for the difficulty again but i guess not
@vedantjha2327
@vedantjha2327 7 ай бұрын
Interesting question.
@realisticlevel2553
@realisticlevel2553 2 ай бұрын
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 :(
@nikolatesla399
@nikolatesla399 2 ай бұрын
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
@vikneshcs
@vikneshcs 18 күн бұрын
Can I convert it to graph and do bfs on node 0
@shivangikhemka4379
@shivangikhemka4379 7 ай бұрын
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)
@priyanshagarwal8490
@priyanshagarwal8490 7 ай бұрын
2055. Plates Between Candles... Next Please..🙏
@sivaramakrishnankn1721
@sivaramakrishnankn1721 7 ай бұрын
boondhi laddoo gang represent
@kevinwang8632
@kevinwang8632 7 ай бұрын
this one was hard
@nikitathacker5096
@nikitathacker5096 6 ай бұрын
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
@impolitebigmac3044
@impolitebigmac3044 7 ай бұрын
I hate leetcode
@staywithmeforever
@staywithmeforever 7 ай бұрын
Double bfs?
@ksvijayan06
@ksvijayan06 7 ай бұрын
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
@sajankumarrajbanshi1158
@sajankumarrajbanshi1158 7 ай бұрын
i love ladoos
@Masterasadumar
@Masterasadumar 7 ай бұрын
lol ladu
@sankhadip_roy
@sankhadip_roy 7 ай бұрын
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
Find the Maximum Sum of Node Values - Leetcode 3068 - Python
17:50
The Number of Beautiful Subsets - Leetcode 2597 - Python
32:34
NeetCodeIO
Рет қаралды 11 М.
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 10 М.
8 Data Structures Every Programmer Should Know
17:09
ForrestKnight
Рет қаралды 224 М.
979. Distribute Coins in Binary Tree | Tricky DFS | Recursion
17:24
Aryan Mittal
Рет қаралды 4,7 М.
Binary Tree Maximum Path Sum - DFS - Leetcode 124 - Python
15:19
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 904 М.
Stop Recommending Clean Code
27:05
ThePrimeTime
Рет қаралды 557 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 240 М.
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 14 М.
Sum of All Subsets XOR Total - Leetcode 1863 - Python
18:26
NeetCodeIO
Рет қаралды 12 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 183 М.