https://leetcode.com/problems/distribute-coins-in-binary-tree/
1. Using DFS:
//8 ms 10.7 MB/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:int distributeCoins(TreeNode* root) {int result = 0;DFS(root, result);return result;}private:int DFS(TreeNode* root, int& result){if(!root) return 0;int l = DFS(root->left, result);int r = DFS(root->right, result);result += abs(l) + abs(r);return root->val + l + r - 1;}};
