Simple Explanation with example in Java
Intuition\n Describe your first thoughts on how to solve this problem. \nIn the case where the game goes until all nodes are selected, you need n/2+1 nodes to w
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn the case where the game goes until all nodes are selected, you need `n/2+1` nodes to win.\n\nBased on the opponent\'s choice, you can select 3 options: the parent, the left branch or the right branch. If there is one of those choices that contains at least the number of nodes to win, it is possible for you to win. If not, it isn\'t possible.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst, walk the Tree to find the opponent\'s choice. Then count the nodes of each child branch. Compare the number of nodes with the number of nodes to win (`n/2+1`) to determine if it\'s possible for you to win.\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n\n    public boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n        //find opponent max potential plays\n        TreeNode opponentNode = find(root, x);\n        int left = count(opponentNode.left);\n        int right = count(opponentNode.right);\n\n        // need at least n/2 + 1 nodes to win\n        int countToWin = n/2+1;\n        //if a child branch has enough nodes to win, select child branch and win\n        if(left >= countToWin || right >= countToWin) return true;\n        //if opponent\'s child branches do not have to win, select parent and win\n        if(left + right + 1 < countToWin) return true;\n        return false;\n    }\n\n    public int count(TreeNode node){\n        if(node == null) {\n            return 0;\n        }\n        int l = count(node.left);\n        int r = count(node.right);\n        return 1 + l + r;\n    }\n\n    public TreeNode find(TreeNode node, int target) {\n        if(node == null) return null;\n        \n        if(node.val == target) {\n            return node;\n        }\n\n        TreeNode l = find(node.left, target);\n        if(l != null) {\n            return l;\n        }\n        TreeNode r = find(node.right, target);\n        if(r != null) {\n            return r;\n        }\n        return null;\n     }\n\n}\n```
['Java']