{"id":5384,"date":"2019-08-04T10:35:33","date_gmt":"2019-08-04T17:35:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5384"},"modified":"2019-08-06T09:33:42","modified_gmt":"2019-08-06T16:33:42","slug":"leetcode-1145-binary-tree-coloring-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1145-binary-tree-coloring-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1145. Binary Tree Coloring Game"},"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 1145. Binary Tree Coloring Game  - \u5237\u9898\u627e\u5de5\u4f5c EP261\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/0MGbvRHYZxc?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>Two players play a turn based game on a binary tree.&nbsp; We are given&nbsp;the&nbsp;<code>root<\/code>&nbsp;of this binary tree, and the number of nodes&nbsp;<code>n<\/code>&nbsp;in the tree.&nbsp;&nbsp;<code>n<\/code>&nbsp;is odd, and&nbsp;each node has a distinct value from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>Initially, the first player names a value&nbsp;<code>x<\/code>&nbsp;with&nbsp;<code>1 &lt;= x &lt;= n<\/code>, and the second player names a value&nbsp;<code>y<\/code>&nbsp;with&nbsp;<code>1 &lt;= y &lt;= n<\/code>&nbsp;and&nbsp;<code>y != x<\/code>.&nbsp; The first player colors the node with value&nbsp;<code>x<\/code>&nbsp;red, and the second player colors the node with value&nbsp;<code>y<\/code>blue.<\/p>\n\n\n\n<p>Then, the players take turns starting with the first player.&nbsp; In each turn, that player chooses a node of their color (red if player 1, blue if player 2) and colors an&nbsp;<strong>uncolored<\/strong>&nbsp;neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)<\/p>\n\n\n\n<p>If (and only if)&nbsp;a player cannot choose such a node in this way, they must pass their turn.&nbsp; If both players pass their turn, the game ends, and the winner is the player that colored more nodes.<\/p>\n\n\n\n<p>You are the second player.&nbsp; If it is possible to choose such a&nbsp;<code>y<\/code>&nbsp;to ensure you win the game, return&nbsp;<code>true<\/code>.&nbsp; If it is not possible, return&nbsp;<code>false<\/code>.<\/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\/08\/01\/1480-binary-tree-coloring-game.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>The second player can choose the node with value 2.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>root<\/code>&nbsp;is the root of a binary tree with&nbsp;<code>n<\/code>&nbsp;nodes and distinct node values from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>.<\/li><li><code>n<\/code>&nbsp;is odd.<\/li><li><code>1 &lt;= x &lt;= n&nbsp;&lt;= 100<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Count size of red&#8217;s subtrees<\/strong><\/p>\n\n\n\n<p>There are two situations that blue can win.<br>1. one of the red&#8217;s subtree has more than n&gt;&gt;1 nodes. Blue colorize the root of the larger subtree.<br>2. red and its children has size less or equal to n&gt;&gt;1. Blue colorize red&#8217;s parent.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool btreeGameWinningMove(TreeNode* root, int n, int x) {\n    int red_left;\n    int red_right;\n    function<int(TreeNode*)> count = [&](TreeNode* node){\n      if (!node) return 0;\n      int l = count(node->left);\n      int r = count(node->right);\n      if (root->val == x) {\n        red_left = l;\n        red_right = r;\n      }\n      return 1 + l + r;\n    };\n    count(root);\n    \/\/ Color red's parent.\n    if (1 + red_left + red_right <= n \/ 2) return true;\n    \/\/ Color the child that has the larger subtree.\n    return max(red_left, red_right) > n \/ 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Two players play a turn based game on a binary tree.&nbsp; We are given&nbsp;the&nbsp;root&nbsp;of this binary tree, and the number of nodes&nbsp;n&nbsp;in the tree.&nbsp;&nbsp;n&nbsp;is odd,&#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":[88,177,28],"class_list":["post-5384","post","type-post","status-publish","format-standard","hentry","category-tree","tag-greedy","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5384","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=5384"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5384\/revisions"}],"predecessor-version":[{"id":5398,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5384\/revisions\/5398"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}