Advertisement
HXXXXJ

979. Distribute Coins in Binary Tree

Apr 13th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.44 KB | None | 0 0
  1.     var move = 0
  2.     func distributeCoins(_ root: TreeNode?) -> Int {
  3.         helper(root)
  4.         return move
  5.     }
  6.    
  7.     func helper( _ root: TreeNode?) -> (Int, Int){
  8.         guard let root = root else { return ( 0 , 0 )}
  9.         let left = helper(root.left)
  10.         let right = helper(root.right)
  11.         move += abs( left.0 - left.1) + abs(right.0 - right.1)
  12.         return (left.0 + right.0 + 1 , left.1 + right.1 + root.val)
  13.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement