bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 979. Distribute Coins in Binary Tree

979. Distribute Coins in Binary Tree

Leetcode Solutions

Depth First Search to Balance Coins in a Binary Tree

  1. Define a recursive DFS function that calculates the excess coins for a node.
  2. For each node, calculate the excess coins for its left and right children recursively.
  3. The number of moves for a node is the absolute value of the sum of excess coins from its left and right children.
  4. Update the total number of moves by adding the number of moves for the current node.
  5. Return the excess coins for the current node to its parent.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...