File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
17 - Binary Tree Data Structure Problems/11 - Sum Tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ private:
3+ pair<int , int > fastSumTree (Node* root){
4+ if (root == NULL ) return {true , 0 };
5+ if (root -> left == NULL && root -> right == NULL ) return {true , root->data };
6+
7+
8+ pair<bool , int > leftAns = fastSumTree (root -> left);
9+ pair<bool , int > rightAns = fastSumTree (root -> right);
10+
11+ bool left = leftAns.first ;
12+ bool right = rightAns.first ;
13+
14+ bool condition = root -> data == leftAns.second + rightAns.second ;
15+
16+ pair<int , int > ans;
17+ if (left && right && condition){
18+ ans.first = true ;
19+ ans.second = 2 * root -> data;
20+ }else {
21+ ans.first = false ;
22+ }
23+
24+
25+ return ans;
26+ }
27+ public:
28+ bool isSumTree (Node* root) {
29+ return fastSumTree (root).first ;
30+ }
31+ };
You can’t perform that action at this time.
0 commit comments