nikunjsoni

1145

May 6th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12. class Solution {
  13. public:
  14.     bool ans=false;
  15.     bool btreeGameWinningMove(TreeNode* root, int n, int x) {
  16.         dfs(root, x, n);
  17.         return ans;
  18.     }
  19.    
  20.     int dfs(TreeNode *root, int x, int n){
  21.         if(!root) return 0;
  22.         int leftSize = dfs(root->left, x, n);
  23.         int rightSize = dfs(root->right, x, n);
  24.         int currCount = leftSize + rightSize + 1;
  25.         if(root->val == x){
  26.             ans = ((leftSize > n-leftSize) || (rightSize > n-rightSize) || (n-currCount > currCount));
  27.         }
  28.         return currCount;
  29.     }
  30.    
  31. };
Advertisement
Add Comment
Please, Sign In to add comment