classSolution{public:boolbtreeGameWinningMove(TreeNode*root,intn,intx){count(root,x);returnmax({leftCount,rightCount,n-leftCount-rightCount-1})>n/2;}private:intleftCount;// # of nodes of n's leftintrightCount;// # of nodes of n's rightintcount(TreeNode*root,intx){if(!root)return0;constintl=count(root->left,x);constintr=count(root->right,x);if(root->val==x){leftCount=l;rightCount=r;}return1+l+r;}};
JAVA
classSolution{publicbooleanbtreeGameWinningMove(TreeNoderoot,intn,intx){count(root,x);returnMath.max(Math.max(leftCount,rightCount),n-leftCount-rightCount-1)>n/2;}privateintleftCount;// # of nodes of n's leftprivateintrightCount;// # of nodes of n's rightprivateintcount(TreeNoderoot,intx){if(root==null)return0;finalintl=count(root.left,x);finalintr=count(root.right,x);if(root.val==x){leftCount=l;rightCount=r;}return1+l+r;}}
Login to Codeflu
Log in to stay update and get notify on new arrivals.