题目链接
Leetcode.1145 二叉树着色游戏 Rating : 1741
题目描述
有两位极客玩家参与了一场「二叉树着色」的游戏。游戏中,给出二叉树的根节点 root,树上总共有 n个节点,且 n为奇数,其中每个节点上的值从 1到 n各不相同。
最开始时:
- 「一号」玩家从 [1, n]中取一个值x( 1 < = x < = n ) (1 <= x <= n) (1<=x<=n);
- 「二号」玩家也从 [1, n]中取一个值y( 1 < = y < = n )且 y ! = x (1 <= y <= n)且 y != x (1<=y<=n)且y!=x。
[一号」玩家给值为 x 的节点染上红色,而「二号」玩家给值为 y的节点染上蓝色。
之后两位玩家轮流进行操作,「一号」玩家先手。每一回合,玩家选择一个被他染过色的节点,将所选节点一个 未着色 的邻节点(即左右子节点、或父节点)进行染色(「一号」玩家染红色,「二号」玩家染蓝色)。
如果(且仅在此种情况下)当前玩家无法找到这样的节点来染色时,其回合就会被跳过。
若两个玩家都没有可以染色的节点时,游戏结束。着色节点最多的那位玩家获得胜利 ✌️。
现在,假设你是「二号」玩家,根据所给出的输入,假如存在一个 y值可以确保你赢得这场游戏,则返回 true;
若无法获胜,就请返回 false。
示例 1 :

输入:root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
输出:true
解释:第二个玩家可以选择值为 2 的节点。
示例 2 :
输入:root = [1,2,3], n = 3, x = 1
输出:false
提示 :
- 树中节点数目为 n
- 1 < = x < = n < = 100 1 <= x <= n <= 100 1<=x<=n<=100
- n是奇数
- 1 < = N o d e . v a l < = n 1 <= Node.val <= n 1<=Node.val<=n
- 树中所有值 互不相同
分析:
先手 x,会将整棵树分成三块。如图:

 我们的 y必然是在这三块其中的一块中选择的(如果存在 y),一个非常明显的选法就是选择与 x相连的结点。
比如选择这一个:

 这样选的话 蓝色就会把这一块涂满,因为这个点已经把道路堵死了。
这样的选法,最终蓝色会占据其中的一块,红色会占据其中的两块。
所以我们只需要 判断 最大的那块的结点个数 是否大于 另外两块节点个数之和 + 1(因为还要加上初始那个红色结点)。
所以 第一步先 dfs找到 x结点 node_x。
 接下来统计三块分别的结点之和:
- node_x左子树的部分:- getcount(node_x->left)
- node_x右子树的部分:- getcount(node_x->right)
- node_x父结点的部分:- getcount(root) - getcount(node_x->left) - getcount(node_x->right) - 1
最后判断,最大的那部分 是否 大于较小的两部分之和即可。
时间复杂度: O ( n ) O(n) O(n)
C++代码:
class Solution {
public:
    TreeNode* node_x = nullptr;
    //统计以 root 为根节点的结点数量之和
    int getCount(TreeNode* root){
        if(root == nullptr) return 0;
        return getCount(root->left) + getCount(root->right) + 1;
    }
    //dfs 找结点 node_x
    void findNode_x(TreeNode* root,int x){
        if(root == nullptr) return;
        if(root->val == x){
            node_x = root;
            return;
        }
        findNode_x(root->left,x);
        findNode_x(root->right,x);
    }
    bool btreeGameWinningMove(TreeNode* root, int n, int x) {
        findNode_x(root,x);
        //左子树的部分
        int b = getCount(node_x->left);
        //右子树的部分
        int c = getCount(node_x->right);
        
        //整棵树总的结点数
        int total = getCount(root);
        //父结点的那部分
        int a = total - b - c - 1;
        int d = max(a,max(b,c));
        return d > total - d;
    }
};
Java代码:
class Solution {
    TreeNode node_x = null;
    int cnt(TreeNode root){
        if(root == null) return 0;
        return cnt(root.left) + cnt(root.right) + 1;
    }
    void findNodeX(TreeNode root,int x){
        if(root == null) return;
        if(root.val == x){
            node_x = root;
            return;
        }
        findNodeX(root.left,x);
        findNodeX(root.right,x);
    }
    public boolean btreeGameWinningMove(TreeNode root, int n, int x) {
        findNodeX(root,x);
        int b = cnt(node_x.left);
        int c = cnt(node_x.right);
        int total = cnt(root);
        int a = total - b - c - 1;
        int d = Math.max(a,Math.max(b,c));
        return d > total - d;
    }
}













![[Java]JavaWeb学习笔记(尚硅谷2020旧版)](https://img-blog.csdnimg.cn/fc449f80dd474e5abf929d0afeee6a2a.png)





