Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- int preCalculate(TreeNode* V) {
- int sum = V->val;
- if (V->left) sum += preCalculate(V->left);
- if (V->right) sum += preCalculate(V->right);
- return sum;
- }
- int isPossible(TreeNode* V, int& total, int& possible) {
- int sum = V->val;
- if (V->left) sum += isPossible(V->left, total, possible);
- if (V->right) sum += isPossible(V->right, total, possible);
- if (2*sum == total) possible = 1;
- return sum;
- }
- int Solution::solve(TreeNode* A) {
- int sum = preCalculate(A), possible = 0;
- isPossible(A, sum, possible);
- return possible;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement