LeetCode 题解工作台

二叉树着色游戏

有两位极客玩家参与了一场「二叉树着色」的游戏。游戏中,给出二叉树的根节点 root ,树上总共有 n 个节点,且 n 为奇数,其中每个节点上的值从 1 到 n 各不相同。 最开始时: 「一号」玩家从 [1, n] 中取一个值 x ( 1 ); 「二号」玩家也从 [1, n] 中取一个值 y ( 1 …

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 二分·树·traversal

bolt

答案摘要

我们先通过 ,找到「一号」玩家着色点 所在的节点,记为 。 接下来,我们统计 的左子树、右子树的节点个数,分别记为 和 ,而 父节点方向上的个数为 $n - l - r - 1$。只要满足 $\max(l, r, n - l - r - 1) > \frac{n}{2}$,则「二号」玩家存在一个必胜策略。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

有两位极客玩家参与了一场「二叉树着色」的游戏。游戏中,给出二叉树的根节点 root,树上总共有 n 个节点,且 n 为奇数,其中每个节点上的值从 1 到 n 各不相同。

最开始时:

  • 「一号」玩家从 [1, n] 中取一个值 x1 <= x <= n);
  • 「二号」玩家也从 [1, n] 中取一个值 y1 <= 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
  • n 是奇数
  • 1 <= Node.val <= n
  • 树中所有值 互不相同
lightbulb

解题思路

方法一:DFS

我们先通过 DFSDFS,找到「一号」玩家着色点 xx 所在的节点,记为 nodenode

接下来,我们统计 nodenode 的左子树、右子树的节点个数,分别记为 llrr,而 nodenode 父节点方向上的个数为 nlr1n - l - r - 1。只要满足 max(l,r,nlr1)>n2\max(l, r, n - l - r - 1) > \frac{n}{2},则「二号」玩家存在一个必胜策略。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是节点总数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def btreeGameWinningMove(self, root: Optional[TreeNode], n: int, x: int) -> bool:
        def dfs(root):
            if root is None or root.val == x:
                return root
            return dfs(root.left) or dfs(root.right)

        def count(root):
            if root is None:
                return 0
            return 1 + count(root.left) + count(root.right)

        node = dfs(root)
        l, r = count(node.left), count(node.right)
        return max(l, r, n - l - r - 1) > n // 2
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Ensure the candidate demonstrates an understanding of binary tree traversal and the importance of adjacency in game theory.

  • question_mark

    Look for an approach where the candidate can explain how Player 1's optimal move is determined in relation to Player 2's options.

  • question_mark

    Evaluate the candidate's ability to simulate turns and state transitions to guarantee a solution with correct time complexity.

warning

常见陷阱

外企场景
  • error

    Overcomplicating the traversal with unnecessary steps that do not contribute to solving the adjacency problem efficiently.

  • error

    Failing to simulate Player 2's move properly, missing out on how to lock down the adjacent nodes strategically.

  • error

    Not considering edge cases where Player 1 could lose due to Player 2’s strategic choices or incorrect assumptions about available moves.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem by increasing the size of the binary tree or adding more complex rules about which nodes can be colored.

  • arrow_right_alt

    Change the initial player to Player 2 and re-evaluate if the strategy changes for Player 1’s optimal move.

  • arrow_right_alt

    Introduce more than two players and evaluate how the game dynamics evolve with additional strategic considerations.

help

常见问题

外企场景

二叉树着色游戏题解:二分·树·traversal | LeetCode #1145 中等