Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- bool ans=false;
- bool btreeGameWinningMove(TreeNode* root, int n, int x) {
- dfs(root, x, n);
- return ans;
- }
- int dfs(TreeNode *root, int x, int n){
- if(!root) return 0;
- int leftSize = dfs(root->left, x, n);
- int rightSize = dfs(root->right, x, n);
- int currCount = leftSize + rightSize + 1;
- if(root->val == x){
- ans = ((leftSize > n-leftSize) || (rightSize > n-rightSize) || (n-currCount > currCount));
- }
- return currCount;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment