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.
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.
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.)
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.
 
Example 1:

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:

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

Difficulty: interview

Starter code:

# 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: TreeNode, n: int, x: int) -> bool:
        

Solutions:
# 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: TreeNode, n: int, x: int) -> bool:
        def count(node):
            if not node:
                return 0
            return 1 + count(node.left) + count(node.right)
        xNode = [0, 0]
        def process(node):
            if node:
                if node.val == x:
                    xNode[0] = count(node.left)
                    xNode[1] = count(node.right)
                else:
                    process(node.left)
                    process(node.right)
            return
        
        process(root)
        player2 = max(xNode[0], xNode[1], n - (xNode[0] + xNode[1] + 1)) # the maximum nodes I can color
        return player2 > n // 2

# 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 __init__(self):
        self.lcnt, self.rcnt = 0, 0
        
    def btreeGameWinningMove(self, root: TreeNode, n: int, x: int) -> bool:
        
        rootCnt = self.dfs(root, x)
        
        return max([rootCnt - self.lcnt - self.rcnt - 1, self.lcnt, self.rcnt]) > n / 2
        
    
    
    def dfs(self, node: TreeNode, x: int) -> int:
        if not node:
            return 0
        left = self.dfs(node.left, x)
        right = self.dfs(node.right, x)
        
        if node.val == x:
            self.lcnt = left
            self.rcnt = right
        
        return 1 + left + right

# 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: TreeNode, n: int, x: int) -> bool:
        # build undirected graph so we can treat both parent and child nodes as
        # neighbors for easy traversal
        graph = collections.defaultdict(list)
        stack = [root]
        while stack:
            node = stack.pop()
            if node.left:
                graph[node.val].append(node.left.val)
                graph[node.left.val].append(node.val)
                stack.append(node.left)
            if node.right:
                graph[node.val].append(node.right.val)
                graph[node.right.val].append(node.val)
                stack.append(node.right)
        
        # for each possible y, we do DFS from x to find how many nodes our 
        # opponent can reach from x. Note that our opponent will not be able to
        # reach any nodes in the subtree rooted at y. If our opponent cannot 
        # reach at least half of all nodes (i.e. n//2), we will win for sure.
        for y in range(1, n+1):
            if y != x:
                visited = set()
                stack = [x]
                while stack:
                    val = stack.pop()
                    visited.add(val)
                    for nei in graph[val]:
                        if nei not in visited and nei != y:
                            stack.append(nei)

                if len(visited) <= n//2:
                    return True
        
        return False
                        
                    
            
                
        
            
            


# 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: TreeNode, n: int, x: int) -> bool:
        c = [0,0]
        def count(root):
            if not root:
                return 0
            l,r = count(root.left),count(root.right)
            if root.val == x:
                c[0],c[1] = l,r
            return l + r + 1
        
        return count(root)/2 < max(max(c),n - sum(c) - 1)

# 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: TreeNode, n: int, x: int) -> bool:
        candidates = []
        def dfs(node):
            if not node:
                return 0
 
            l = dfs(node.left)
            r = dfs(node.right)
            if node.val == x:
                l and candidates.append(l)
                r and candidates.append(r)
            return 1 + l + r
        # candidates now contain left and right subtree of the   node with val x
        # Get the length of subtree of parent node
        dfs(root)
        # print(candidates)
        # if root.val != x:
        candidates.append(n - (1 + sum(candidates)))
        return any(mine > (n-mine) for mine in candidates)
        
  
                
                
                


# 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: TreeNode, n: int, x: int) -> bool:
        def countNodes(node):
            left = 0
            if node == None:
                return 0
            if node.left:
                left = countNodes(node.left)
            right = 0
            if node.right:
                right = countNodes(node.right)
            return left+right+1
        
        def searchNode(node, x):
            if node.val == x:
                return node
            if node.left:
                tmp = searchNode(node.left, x)
                if tmp:
                    return tmp
            if node.right:
                tmp = searchNode(node.right, x)
                if tmp:
                    return tmp
            return None
        
        node = searchNode(root, x)
        left = countNodes(node.left)
        right = countNodes(node.right)
        parent = n - left - right -1
        if parent > n-parent or left > n- left or right > n - right:
            return True
        return False

# 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, n, x):
        \"\"\"
        :type root: TreeNode
        :type n: int
        :type x: int
        :rtype: bool
        \"\"\"
        self.l, self.r = None, None
        def numNodes(root):
            if not root:
                return 0 
            l = numNodes(root.left)
            r = numNodes(root.right)
            if root.val == x:
                self.l = l
                self.r = r
            return l+r+1
        
        numNodes(root)
        max_score = max(self.l, self.r, n-self.l-self.r-1)
        
        return max_score > n/2

# 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: TreeNode, n: int, x: int) -> bool:
        c = [0, 0]
        
        def count(node):
            if not node: return 0
            l, r = count(node.left), count(node.right)
            
            if node.val == x:
                c[0], c[1] = l, r
                
            return l + r + 1
        
        return count(root) / 2 < max(max(c), n - sum(c) - 1)
        


# 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: TreeNode, n: int, x: int) -> bool:
        graph = defaultdict(list)
        
        def dfs(s):
            if not s:
                return
            for child in [s.left, s.right]:
                if not child:
                    continue
                graph[child.val].append(s.val)
                graph[s.val].append(child.val)
                dfs(child)
        dfs(root)
        
        visited = set()
        visited.add(x)
        
        def dfs_graph(u):
            res = 1
            visited.add(u)
            for nei in graph[u]:
                if nei not in visited:
                    res += dfs_graph(nei)
            return res
        
        candidates = [dfs_graph(nei) for nei in graph[x]]
        if not candidates:
            return False
        res = max(candidates)
        
        if res > n - res:
            return True
        else:
            return False

# 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: TreeNode, n: int, x: int) -> bool:
        c = [0,0]
        def count(root):
            if not root: return 0
            l,r = count(root.left),count(root.right)
            if root.val == x:
                c[0],c[1] = l,r
                
            return l + r + 1
        return count(root)/2 < max(max(c),n - sum(c) - 1)

# 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: TreeNode, n: int, x: int) -> bool:
        # for all possible y, we need to check if we can win
        # we can return early if we can do that
        
        # now what to do after selecting a y
        # we have three choices:
        # select left, right or parent 
        # we can simulate what happens in each case
        # we can return True if any of those choices
        # return True
        # we also have to simulate what happens if it is not
        # our turn, in that case, we have to choose the minimum
        # i.e. return false if anything is false
        # since we are trying to maximize our profit
        
        
        # we also need a way to simulate which node is colored what 
        # atleast temporarily
        
        # we can store a tuple for nodes
        # if 0, uncolored
        # if 1, blue
        # if -1, red
        
        # exit conditions
        # if no nodes left to be colored
        # 
        
        if not root:
            return False
        
        from collections import defaultdict
        
        graph = defaultdict(list)
        
        def convert_tree_to_graph(root, parent=None):
            if not root:
                return None
            if parent:
                # creating bi-directional edges
                graph[parent].append(root)
                graph[root].append(parent)
            if root.left:
                convert_tree_to_graph(root.left, root)
            if root.right:
                convert_tree_to_graph(root.right, root)
                    
        convert_tree_to_graph(root)
    
        # now we have the graph, we just need to find the node that player 1 selected
        # and count its trees in each direction
        node = None
        for g, v in list(graph.items()):
            if g.val == x:
                node = g
                break
        
        def count_nodes_in_subtree(node, visited):
            #return count
            if node in visited or node.val==x:
                return 0
            visited.add(node)
            count = 0
            for neighbor in graph[node]:
                if neighbor not in visited:
                    count += count_nodes_in_subtree(neighbor, visited)
            return count + 1 
                    
        subtrees = []
        max_subtree = -1
        #print (node, subtrees)
        for subtree in graph[node]:
            ans = count_nodes_in_subtree(subtree, set())
            subtrees.append(ans)
            max_subtree = max(max_subtree, ans)
        
        return n - max_subtree < max_subtree
            
            
        if not subtrees:
            return False
        
        if len(subtrees)== 1 and subtrees[0]>1 : # only 1 subtree and we chose it
            return True 
        
        
        
        return False
            
        
        
        
                    
                    
        
        


# 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: TreeNode, n: int, x: int) -> bool:
        #https://leetcode.com/problems/binary-tree-coloring-game/discuss/797574/Python-3-or-DFS-or-One-pass-and-Three-pass-or-Explanation
        
        if not root:
            return False
        
        from collections import defaultdict
        
        graph = defaultdict(list)
        
        def convert_tree_to_graph(root, parent=None):
            if not root:
                return None
            if parent:
                # creating bi-directional edges
                graph[parent].append(root)
                graph[root].append(parent)
            if root.left:
                convert_tree_to_graph(root.left, root)
            if root.right:
                convert_tree_to_graph(root.right, root)
                    
        convert_tree_to_graph(root)
    
        # now we have the graph, we just need to find the node that player 1 selected
        # and count its trees in each direction
        node = None
        for g, v in list(graph.items()):
            if g.val == x:
                node = g
                break
        
        def count_nodes_in_subtree(node, visited):
            #return count
            if node in visited or node.val==x:
                return 0
            visited.add(node)
            count = 0
            for neighbor in graph[node]:
                if neighbor not in visited:
                    count += count_nodes_in_subtree(neighbor, visited)
            return count + 1 
                    
        subtrees = []
        max_subtree = -1
        #print (node, subtrees)
        for subtree in graph[node]:
            ans = count_nodes_in_subtree(subtree, set())
            subtrees.append(ans)
            max_subtree = max(max_subtree, ans)
        
        return n - max_subtree < max_subtree
        
        
        if not subtrees:
            return False
        
        if len(subtrees)== 1 and subtrees[0]>1 : # only 1 subtree and we chose it
            return True 
        
        return False