JAVA | Explained | beginner friendly
\n\t// When you find the selected node, there are three different paths you can block: left ,right or parent \n\t// if i color left(a) node blue, then red perso
\n\t// When you find the selected node, there are three different paths you can block: left ,right or parent \n\t// if i color left(a) node blue, then red person can color all right and parent nodes red\n\t// if i color right (b) node blue, then red person can color all left and parent nodes red\n\t// if i color parent(c) node blue, then red person can color all right and left nodes red\n\t// so i can only win if i choose a node which contains elements greater than the count of elements in otther 2 nodes i.e a>b+c i color a node blue, or b>a+c i color b node blue , or c>a+b i color c node blue\n\t// if none of these 3 conditions hold true then red person wins bcoz he has more nodes to color\n\tclass Solution {\n\t\tpublic boolean btreeGameWinningMove(TreeNode root, int n, int x) {\n\t\t\tif(root==null) return false;\n\t\t\tif(root.val==x){\n\t\t\t\tint a=countofnodes(root.left);\n\t\t\t\tint b=countofnodes(root.right);\n\t\t\t\tint c=n-a-b-1; // 1 for root\n\t\t\t\tif(a>b+c || b>c+a || c>a+b) return true;\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tboolean left=btreeGameWinningMove(root.left,n,x);\n\t\t\tif(left) return true;\n\t\t\tboolean right=btreeGameWinningMove(root.right,n,x);\n\t\t\tif(right) return true;\n\n\t\t\treturn false;\n\t\t}\n\t\tpublic int countofnodes(TreeNode root){\n\t\t\tif(root==null) return 0;\n\t\t\treturn countofnodes(root.left)+countofnodes(root.right)+1;\n\t\t}\n\t}\n
['Java']