{"id":60,"date":"2020-03-22T07:16:23","date_gmt":"2020-03-22T07:16:23","guid":{"rendered":"http:\/\/feellikelearning.com\/?p=60"},"modified":"2020-03-22T08:52:58","modified_gmt":"2020-03-22T08:52:58","slug":"cn-leetcode-1145-bt-coloring-game","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/","title":{"rendered":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5"},"content":{"rendered":"\n<p>\u8fd9\u9898\u63cf\u8ff0\u5de8\u957f\u65e0\u6bd4\uff0c\u53ef\u4ee5\u5f53\u4e00\u7bc7\u9605\u8bfb\u7406\u89e3\u6765\u505a\u4e86\uff0c\u770b\u5b8c\u4e5f\u4e0d\u662f\u5f88\u5bb9\u6613\u5c31\u80fd\u60f3\u5230\u662f\u5c5e\u4e8e\u54ea\u7c7b\u7684\u9898\u76ee\uff0c\u56fe\u4e5f\u753b\u7684\u975e\u5e38\u6709\u8ff7\u60d1\u6027\uff0c\u84dd\u8272\u5e94\u8be5\u753b\u57281\u4f4d\u7f6e\u80fd\u591a\u5360\u4e00\u4e2a\u3002\u70ed\u7231\u505a\u8111\u7b4b\u6025\u8f6c\u5f2f\u7684\u540c\u5b66\u53ef\u4ee5\u6311\u6218\u4e00\u4e0b\uff0c\u6ca1\u65f6\u95f4\u7684\u540c\u5b66\u5c31\u76f4\u63a5\u8bb0\u4f4f\u7ed3\u8bba\u5c31\u597d\uff0c\u84dd\u8272\u7684\u53ef\u80fd\u9009\u62e9\u5176\u5b9e\u53ea\u67093\u4e2a\uff1a\u7ea2\u8272\u7684\u5de6\u8fb9\uff0c\u53f3\u8fb9\uff0c\u548c\u4e0a\u9762\uff0c\u5206\u522b\u5207\u65ad\u548c\u5360\u9886\u5de6\u53f3\u4e0a\u4e09\u90e8\u5206\uff0c\u7136\u540e\u770b\u770b\u6bcf\u90e8\u5206\u80fd\u4e0d\u80fd\u8d85\u8fc7\u4e00\u534a\u5c31\u597d\u3002\u6709\u4e86\u8fd9\u4e2a\u601d\u8def\uff0c\u7528DFS\u8dd1\u4e00\u6b21\u5c31\u884c\uff0c\u65f6\u95f4\u662fO(n)\uff0c\u6ca1\u6709explicit extra space\uff0c\u4f46\u7b97\u4e0aDFS\u9012\u5f52\u5360\u7528stack\u4e5f\u662fO(n)<\/p>\n\n\n\n<p>Python \u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def btreeGameWinningMove(self, root, n, x):\n        \"\"\"\n        :type root: TreeNode\n        :type n: int\n        :type x: int\n        :rtype: bool\n        \"\"\"\n        xnode = self.find(root, x)\n        (xcount, lcount, rcount) = self.tree_stats(xnode)\n        half = n \/ 2\n        if lcount > half or rcount > half or n - xcount > half:\n            return True\n        return False\n        \n    \n    def find(self, root, x):\n        if root is None:\n            return None\n        if root.val == x:\n            return root\n        l = self.find(root.left, x)\n        if l is not None:\n            return l\n        else:\n            return self.find(root.right, x)\n        \n    def tree_stats(self, root):\n        if root is None:\n            return (0, 0, 0)\n        l = self.tree_stats(root.left)\n        r = self.tree_stats(root.right)\n        return (l[0] + r[0] + 1, l[0], r[0])<\/code><\/pre>\n\n\n\n<p>\u8fd8\u6709\u4e00\u79cd\u5199\u6cd5\uff0c\u53ef\u4ee5\u628a\u67e5\u627e\u548c\u7b97\u5b50\u6811\u8282\u70b9\u4e2a\u6570\u5408\u5e76\u6210\u4e00\u4e2aDFS\uff0ctrick\u5c31\u662f\u7528\u4e00\u4e2acontainer\u53c2\u6570\u8bb0\u5f55\u5f53\u627e\u5230x\u65f6\uff0c\u4ed6\u7684\u5de6\u4e2d\u4e0a\u80fd\u63a7\u5236\u7684\u8282\u70b9\u6811\uff0c\u5176\u4ed6\u90e8\u5206\u5c31\u662f\u6b63\u5e38\u7684DFS\u3002\u80fd\u7f29\u77ed\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>Python \u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution(object):\n    def btreeGameWinningMove(self, root, n, x):\n        \"\"\"\n        :type root: TreeNode\n        :type n: int\n        :type x: int\n        :rtype: bool\n        \"\"\"\n        ans = []\n        self.tree_stats(root, x, n, ans)\n        return ans[0] > n \/ 2\n             \n    def tree_stats(self, root, x, n, ans):\n        if root is None:\n            return 0\n        l = self.tree_stats(root.left, x, n, ans)\n        r = self.tree_stats(root.right, x, n, ans)\n        cnt = l + r + 1\n        if root.val == x:\n            ans.append(max(l, r, n - cnt))\n        return cnt<\/code><\/pre>\n\n\n\n<p>Java \u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\n    public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n        int [] ans = new int[1];\n        nodes(root, n, x, ans);\n        return ans[0] > n \/ 2;\n    }\n    \n    public int nodes(TreeNode root, int n, int x, int[] ans) {\n        if (root == null) {\n            return 0;\n        }\n        int l = nodes(root.left, n, x, ans);\n        int r = nodes(root.right, n, x, ans);\n        int cnt = l + r + 1;\n        if (root.val == x) {\n            ans[0] = Math.max(Math.max(l, r), n - cnt);\n        }\n        return cnt;\n    }\n}<\/code><\/pre>\n\n\n\n<p>Scala \u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>object Solution {\n    def btreeGameWinningMove(root: TreeNode, n: Int, x: Int): Boolean = {\n        val ans: Array[Int] = new Array(1)\n        nodes(root, n, x, ans)\n        return ans(0) > n \/ 2\n    }\n    \n    def nodes(root: TreeNode, n: Int, x: Int, ans: Array[Int]): Int = {\n        if (root == null) {\n            0\n        } else {\n            val l: Int = nodes(root.left, n, x, ans)\n            val r: Int = nodes(root.right, n, x, ans)\n            val cnt: Int = l + r + 1\n            if (root.value == x) {\n                ans(0) = scala.math.max(scala.math.max(l, r), n - cnt) \n            }\n            cnt\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Scala\u548cJava\u57fa\u672c\u4e00\u6837\uff0c\u5c31\u662f\u6ce8\u610f\u6ca1\u6709return statement\uff0c\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5c31\u662freturn\u503c\u3002\u8fd8\u6709\u5b9a\u4e49\u51fd\u6570\u8bb0\u5f97\u8981\u52a0 =<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9898\u63cf\u8ff0\u5de8\u957f\u65e0\u6bd4\uff0c\u53ef\u4ee5\u5f53\u4e00\u7bc7\u9605\u8bfb\u7406\u89e3\u6765\u505a\u4e86\uff0c\u770b\u5b8c\u4e5f\u4e0d\u662f\u5f88\u5bb9\u6613\u5c31\u80fd\u60f3\u5230\u662f\u5c5e\u4e8e\u54ea\u7c7b\u7684\u9898\u76ee\uff0c\u56fe\u4e5f\u753b\u7684\u975e\u5e38\u6709\u8ff7\u60d1\u6027\uff0c\u84dd\u8272\u5e94\u8be5\u753b\u57281\u4f4d\u7f6e\u80fd\u591a\u5360\u4e00\u4e2a\u3002\u70ed\u7231\u505a\u8111\u7b4b\u6025\u8f6c\u5f2f\u7684\u540c\u5b66\u53ef\u4ee5\u6311\u6218\u4e00\u4e0b\uff0c\u6ca1\u65f6\u95f4\u7684\u540c\u5b66\u5c31\u76f4\u63a5\u8bb0\u4f4f\u7ed3\u8bba\u5c31\u597d\uff0c\u84dd\u8272\u7684\u53ef\u80fd\u9009\u62e9\u5176\u5b9e\u53ea\u67093\u4e2a\uff1a\u7ea2\u8272\u7684\u5de6\u8fb9\uff0c\u53f3\u8fb9\uff0c\u548c\u4e0a\u9762\uff0c\u5206\u522b\u5207\u65ad\u548c\u5360\u9886\u5de6\u53f3\u4e0a\u4e09\u90e8\u5206\uff0c\u7136\u540e\u770b\u770b\u6bcf\u90e8\u5206\u80fd\u4e0d\u80fd\u8d85\u8fc7\u4e00\u534a\u5c31\u597d\u3002\u6709\u4e86\u8fd9\u4e2a\u601d\u8def\uff0c\u7528DFS\u8dd1\u4e00\u6b21\u5c31\u884c\uff0c\u65f6\u95f4\u662fO(n)\uff0c\u6ca1\u6709explicit extra space\uff0c\u4f46\u7b97\u4e0aDFS\u9012\u5f52\u5360\u7528stack\u4e5f\u662fO(n) Python \u5b9e\u73b0 \u8fd8\u6709\u4e00\u79cd\u5199\u6cd5\uff0c\u53ef\u4ee5\u628a\u67e5\u627e\u548c\u7b97\u5b50\u6811\u8282\u70b9\u4e2a\u6570\u5408\u5e76\u6210\u4e00\u4e2aDFS\uff0ctrick\u5c31\u662f\u7528\u4e00\u4e2acontainer\u53c2\u6570\u8bb0\u5f55\u5f53\u627e\u5230x\u65f6\uff0c\u4ed6\u7684\u5de6\u4e2d\u4e0a\u80fd\u63a7\u5236\u7684\u8282\u70b9\u6811\uff0c\u5176\u4ed6\u90e8\u5206\u5c31\u662f\u6b63\u5e38\u7684DFS\u3002\u80fd\u7f29\u77ed\u4ee3\u7801\u3002 Python \u5b9e\u73b0 Java \u5b9e\u73b0 Scala \u5b9e\u73b0 Scala\u548cJava\u57fa\u672c\u4e00\u6837\uff0c\u5c31\u662f\u6ce8\u610f\u6ca1\u6709return statement\uff0c\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5c31\u662freturn\u503c\u3002\u8fd8\u6709\u5b9a\u4e49\u51fd\u6570\u8bb0\u5f97\u8981\u52a0 =<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u8fd9\u9898\u63cf\u8ff0\u5de8\u957f\u65e0\u6bd4\uff0c\u53ef\u4ee5\u5f53\u4e00\u7bc7\u9605\u8bfb\u7406\u89e3\u6765\u505a\u4e86\uff0c\u770b\u5b8c\u4e5f\u4e0d\u662f\u5f88\u5bb9\u6613\u5c31\u80fd\u60f3\u5230\u662f\u5c5e\u4e8e\u54ea\u7c7b\u7684\u9898\u76ee\uff0c\u56fe\u4e5f\u753b\u7684\u975e\u5e38\u6709\u8ff7\u60d1\u6027\uff0c\u84dd\u8272\u5e94\u8be5\u753b\u57281\u4f4d\u7f6e\u80fd\u591a\u5360\u4e00\u4e2a\u3002\u70ed\u7231\u505a\u8111\u7b4b\u6025\u8f6c\u5f2f\u7684\u540c\u5b66\u53ef\u4ee5\u6311\u6218\u4e00\u4e0b\uff0c\u6ca1\u65f6\u95f4\u7684\u540c\u5b66\u5c31\u76f4\u63a5\u8bb0\u4f4f\u7ed3\u8bba\u5c31\u597d\uff0c\u84dd\u8272\u7684\u53ef\u80fd\u9009\u62e9\u5176\u5b9e\u53ea\u67093\u4e2a\uff1a\u7ea2\u8272\u7684\u5de6\u8fb9\uff0c\u53f3\u8fb9\uff0c\u548c\u4e0a\u9762\uff0c\u5206\u522b\u5207\u65ad\u548c\u5360\u9886\u5de6\u53f3\u4e0a\u4e09\u90e8\u5206\uff0c\u7136\u540e\u770b\u770b\u6bcf\u90e8\u5206\u80fd\u4e0d\u80fd\u8d85\u8fc7\u4e00\u534a\u5c31\u597d\u3002\u6709\u4e86\u8fd9\u4e2a\u601d\u8def\uff0c\u7528DFS\u8dd1\u4e00\u6b21\u5c31\u884c\uff0c\u65f6\u95f4\u662fO(n)\uff0c\u6ca1\u6709explicit extra space\uff0c\u4f46\u7b97\u4e0aDFS\u9012\u5f52\u5360\u7528stack\u4e5f\u662fO(n) Python \u5b9e\u73b0 \u8fd8\u6709\u4e00\u79cd\u5199\u6cd5\uff0c\u53ef\u4ee5\u628a\u67e5\u627e\u548c\u7b97\u5b50\u6811\u8282\u70b9\u4e2a\u6570\u5408\u5e76\u6210\u4e00\u4e2aDFS\uff0ctrick\u5c31\u662f\u7528\u4e00\u4e2acontainer\u53c2\u6570\u8bb0\u5f55\u5f53\u627e\u5230x\u65f6\uff0c\u4ed6\u7684\u5de6\u4e2d\u4e0a\u80fd\u63a7\u5236\u7684\u8282\u70b9\u6811\uff0c\u5176\u4ed6\u90e8\u5206\u5c31\u662f\u6b63\u5e38\u7684DFS\u3002\u80fd\u7f29\u77ed\u4ee3\u7801\u3002 Python \u5b9e\u73b0 Java \u5b9e\u73b0 Scala \u5b9e\u73b0 Scala\u548cJava\u57fa\u672c\u4e00\u6837\uff0c\u5c31\u662f\u6ce8\u610f\u6ca1\u6709return statement\uff0c\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5c31\u662freturn\u503c\u3002\u8fd8\u6709\u5b9a\u4e49\u51fd\u6570\u8bb0\u5f97\u8981\u52a0 =\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-22T07:16:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-22T08:52:58+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5\",\"datePublished\":\"2020-03-22T07:16:23+00:00\",\"dateModified\":\"2020-03-22T08:52:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\"},\"wordCount\":29,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\",\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2020-03-22T07:16:23+00:00\",\"dateModified\":\"2020-03-22T08:52:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant","og_locale":"en_US","og_type":"article","og_title":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning","og_description":"\u8fd9\u9898\u63cf\u8ff0\u5de8\u957f\u65e0\u6bd4\uff0c\u53ef\u4ee5\u5f53\u4e00\u7bc7\u9605\u8bfb\u7406\u89e3\u6765\u505a\u4e86\uff0c\u770b\u5b8c\u4e5f\u4e0d\u662f\u5f88\u5bb9\u6613\u5c31\u80fd\u60f3\u5230\u662f\u5c5e\u4e8e\u54ea\u7c7b\u7684\u9898\u76ee\uff0c\u56fe\u4e5f\u753b\u7684\u975e\u5e38\u6709\u8ff7\u60d1\u6027\uff0c\u84dd\u8272\u5e94\u8be5\u753b\u57281\u4f4d\u7f6e\u80fd\u591a\u5360\u4e00\u4e2a\u3002\u70ed\u7231\u505a\u8111\u7b4b\u6025\u8f6c\u5f2f\u7684\u540c\u5b66\u53ef\u4ee5\u6311\u6218\u4e00\u4e0b\uff0c\u6ca1\u65f6\u95f4\u7684\u540c\u5b66\u5c31\u76f4\u63a5\u8bb0\u4f4f\u7ed3\u8bba\u5c31\u597d\uff0c\u84dd\u8272\u7684\u53ef\u80fd\u9009\u62e9\u5176\u5b9e\u53ea\u67093\u4e2a\uff1a\u7ea2\u8272\u7684\u5de6\u8fb9\uff0c\u53f3\u8fb9\uff0c\u548c\u4e0a\u9762\uff0c\u5206\u522b\u5207\u65ad\u548c\u5360\u9886\u5de6\u53f3\u4e0a\u4e09\u90e8\u5206\uff0c\u7136\u540e\u770b\u770b\u6bcf\u90e8\u5206\u80fd\u4e0d\u80fd\u8d85\u8fc7\u4e00\u534a\u5c31\u597d\u3002\u6709\u4e86\u8fd9\u4e2a\u601d\u8def\uff0c\u7528DFS\u8dd1\u4e00\u6b21\u5c31\u884c\uff0c\u65f6\u95f4\u662fO(n)\uff0c\u6ca1\u6709explicit extra space\uff0c\u4f46\u7b97\u4e0aDFS\u9012\u5f52\u5360\u7528stack\u4e5f\u662fO(n) Python \u5b9e\u73b0 \u8fd8\u6709\u4e00\u79cd\u5199\u6cd5\uff0c\u53ef\u4ee5\u628a\u67e5\u627e\u548c\u7b97\u5b50\u6811\u8282\u70b9\u4e2a\u6570\u5408\u5e76\u6210\u4e00\u4e2aDFS\uff0ctrick\u5c31\u662f\u7528\u4e00\u4e2acontainer\u53c2\u6570\u8bb0\u5f55\u5f53\u627e\u5230x\u65f6\uff0c\u4ed6\u7684\u5de6\u4e2d\u4e0a\u80fd\u63a7\u5236\u7684\u8282\u70b9\u6811\uff0c\u5176\u4ed6\u90e8\u5206\u5c31\u662f\u6b63\u5e38\u7684DFS\u3002\u80fd\u7f29\u77ed\u4ee3\u7801\u3002 Python \u5b9e\u73b0 Java \u5b9e\u73b0 Scala \u5b9e\u73b0 Scala\u548cJava\u57fa\u672c\u4e00\u6837\uff0c\u5c31\u662f\u6ce8\u610f\u6ca1\u6709return statement\uff0c\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5c31\u662freturn\u503c\u3002\u8fd8\u6709\u5b9a\u4e49\u51fd\u6570\u8bb0\u5f97\u8981\u52a0 =","og_url":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant","og_site_name":"Feel Like Learning","article_published_time":"2020-03-22T07:16:23+00:00","article_modified_time":"2020-03-22T08:52:58+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5","datePublished":"2020-03-22T07:16:23+00:00","dateModified":"2020-03-22T08:52:58+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant"},"wordCount":29,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant","url":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant","name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5 | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2020-03-22T07:16:23+00:00","dateModified":"2020-03-22T08:52:58+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2020\/03\/22\/cn-leetcode-1145-bt-coloring-game\/?variant=zh-hant#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"\u7b97\u6cd5\u7ec3\u4e60 Leetcode \u529b\u6263 1145. Binary Tree Coloring Game \u89e3\u6cd5"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":7,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}