functionbtreeGameWinningMove(root: TreeNode | null, n: number, x: number): boolean { const node = findNode(root, x) const l = countNodes(node.left) const r = countNodes(node.right) const half = Math.floor(n / 2) if (l > half || r > half || l + r < half) { returntrue } returnfalse };
functionfindNode(node: TreeNode, target: number) {{ if (node === null) returnnull if (node.val === target) { return node } const l = findNode(node.left, target) const r = findNode(node.right, target) return l ? l : r }}
functioncountNodes(node: TreeNode): number { if (node === null) return0 returncountNodes(node.left) + countNodes(node.right) + 1 }