{"id":4672,"date":"2019-01-20T01:20:55","date_gmt":"2019-01-20T09:20:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4672"},"modified":"2019-01-21T01:03:33","modified_gmt":"2019-01-21T09:03:33","slug":"leetcode-979-distribute-coins-in-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-979-distribute-coins-in-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 979. Distribute Coins in Binary Tree"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 979. Distribute Coins in Binary Tree  - \u5237\u9898\u627e\u5de5\u4f5c EP243\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/zQqku1AXVF8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree with&nbsp;<code>N<\/code>&nbsp;nodes, each&nbsp;<code>node<\/code>&nbsp;in the tree has&nbsp;<code>node.val<\/code>coins, and there are&nbsp;<code>N<\/code>&nbsp;coins total.<\/p>\n\n\n\n<p>In one move, we may choose two adjacent nodes and move one coin from one node to another.&nbsp; (The move may be from parent to child, or from child to parent.)<\/p>\n\n\n\n<p>Return the number of moves required to make every node have exactly one coin.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/18\/tree1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[3,0,0]\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>From the root of the tree, we move one coin to its left child, and one coin to its right child.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/18\/tree2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[0,3,0]\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>From the left child of the root, we move two coins to the root [taking two moves].  Then, we move one coin from the root of the tree to the right child.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/18\/tree3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,0,2]\n<strong>Output: <\/strong>2\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/01\/18\/tree4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,0,0,null,3]\n<strong>Output: <\/strong>4\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1&lt;= N &lt;= 100<\/code><\/li><li><code>0 &lt;= node.val &lt;= N<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Compute the balance of left\/right subtree, ans += abs(balance(left)) + abs(balance(right))<br>balance(root) = balance(left) +  balance(right) + root.val &#8211; 1<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">class Solution {\npublic:\n  int distributeCoins(TreeNode* root) {    \n    int ans = 0;\n    balance(root, ans);\n    return ans;\n  }\nprivate:\n  int balance(TreeNode* root, int&amp; ans) {\n    if (!root) return 0;\n    int l = balance(root-&gt;left, ans);\n    int r = balance(root-&gt;right, ans);\n    ans += abs(l) + abs(r);\n    return l + r + root-&gt;val - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree with&nbsp;N&nbsp;nodes, each&nbsp;node&nbsp;in the tree has&nbsp;node.valcoins, and there are&nbsp;N&nbsp;coins total. In one move, we may choose two adjacent nodes and move&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[177,17,28],"class_list":["post-4672","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4672"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4672\/revisions"}],"predecessor-version":[{"id":4683,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4672\/revisions\/4683"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}