Skip to content

Commit 1d01575

Browse files
authored
Create main.cpp
1 parent fb0be14 commit 1d01575

File tree

1 file changed

+31
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/11 - Sum Tree

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
 (0)