[Swift]LeetCode1145. 二叉樹着色遊戲 | Binary Tree Coloring Game

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-yugsjkor-kr.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Two players play a turn based game on a binary tree.  We are given the root of this binary tree, and the number of nodes n in the tree.  n is odd, and each node has a distinct value from 1 to n.node

Initially, the first player names a value x with 1 <= x <= n, and the second player names a value y with 1 <= y <= n and y != x.  The first player colors the node with value x red, and the second player colors the node with value y blue.git

Then, the players take turns starting with the first player.  In each turn, that player chooses a node of their color (red if player 1, blue if player 2) and colors an uncolored neighbor of the chosen node (either the left child, right child, or parent of the chosen node.)github

If (and only if) a player cannot choose such a node in this way, they must pass their turn.  If both players pass their turn, the game ends, and the winner is the player that colored more nodes.微信

You are the second player.  If it is possible to choose such a y to ensure you win the game, return true.  If it is not possible, return false.this

Example 1:spa

Input: root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
Output: True
Explanation: The second player can choose the node with value 2.

Constraints:3d

  • root is the root of a binary tree with n nodes and distinct node values from 1 to n.
  • 1 <= x <= n <= 100

有兩位極客玩家參與了一場「二叉樹着色」的遊戲。遊戲中,給出二叉樹的根節點 root,樹上總共有 n 個節點,且 n 爲奇數,其中每一個節點上的值從 1 到 n 各不相同。code

 

遊戲從「一號」玩家開始(「一號」玩家爲紅色,「二號」玩家爲藍色),最開始時,htm

「一號」玩家從 [1, n] 中取一個值 x1 <= x <= n);

「二號」玩家也從 [1, n] 中取一個值 y1 <= y <= n)且 y != x

「一號」玩家給值爲 x 的節點染上紅色,而「二號」玩家給值爲 y 的節點染上藍色。

 

以後兩位玩家輪流進行操做,每一回合,玩家選擇一個他以前塗好顏色的節點,將所選節點一個 未着色 的鄰節點(即左右子節點、或父節點)進行染色。

若是當前玩家沒法找到這樣的節點來染色時,他的回合就會被跳過。

若兩個玩家都沒有能夠染色的節點時,遊戲結束。着色節點最多的那位玩家得到勝利 ✌️。 

如今,假設你是「二號」玩家,根據所給出的輸入,假如存在一個 y 值能夠確保你贏得這場遊戲,則返回 true;若沒法獲勝,就請返回 false

 

示例:

輸入:root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
輸出:True
解釋:第二個玩家能夠選擇值爲 2 的節點。 

提示:

  • 二叉樹的根節點爲 root,樹上由 n 個節點,節點上的值從 1 到 n 各不相同。
  • n 爲奇數。
  • 1 <= x <= n <= 100

Runtime: 12 ms
Memory Usage: 20.8 MB
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {    
15     func btreeGameWinningMove(_ root: TreeNode?, _ n: Int, _ x: Int) -> Bool {
16         var ls:Int = 0
17         var rs:Int = 0
18         dfs(root,x,&ls,&rs)
19         return max(max(ls,rs),n - ls - rs - 1) > (n>>1)        
20     }
21     
22     func work(_ root: TreeNode?) -> Int
23     {
24         if root == nil {return 0}
25         return work(root!.left) + work(root!.right) + 1        
26     }
27     
28     func dfs(_ root: TreeNode?,_ x:Int,_ ls:inout Int,_ rs:inout Int)
29     {
30         if root == nil {return}
31         if root!.val == x
32         {
33             ls = work(root!.left)
34             rs = work(root!.right)
35         }
36         else
37         {
38             dfs(root!.left,x,&ls,&rs)
39             dfs(root!.right,x,&ls,&rs)
40         }
41     }
42 }
相關文章
相關標籤/搜索