Skip to content

Commit d5a49e4

Browse files
authored
Create README.md
1 parent b7e019b commit d5a49e4

File tree

1 file changed

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

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<h1 align='center'>Sum - Tree</h1>
2+
3+
## Problem Statement
4+
5+
**Problem URL :** [Sum Tree](https://www.geeksforgeeks.org/problems/sum-tree/1)
6+
7+
![image](https://github.com/user-attachments/assets/f7b5db2f-4414-47fc-bfd8-dbd8b721ec17)
8+
![image](https://github.com/user-attachments/assets/e6f215ea-3b7f-431b-859a-ad3ddb48ec24)
9+
10+
## Problem Explanation
11+
The problem requires us to determine whether a given binary tree is a **Sum Tree**. A binary tree is defined as a Sum Tree if, for every node in the tree, the value of the node is equal to the sum of the values of its left and right children. Additionally, leaf nodes are also considered Sum Trees as their children are `NULL` (which sum to zero).
12+
13+
#### Examples:
14+
15+
1. **Example 1**:
16+
- **Input Tree**:
17+
```
18+
26
19+
/ \
20+
10 3
21+
/ \ \
22+
4 6 3
23+
```
24+
- **Output**: `true` (Each node's value is the sum of its children's values)
25+
26+
2. **Example 2**:
27+
- **Input Tree**:
28+
```
29+
10
30+
/ \
31+
5 5
32+
```
33+
- **Output**: `false` (The root's value is not equal to the sum of its children)
34+
35+
3. **Example 3**:
36+
- **Input Tree**:
37+
```
38+
5
39+
/ \
40+
3 2
41+
```
42+
- **Output**: `false` (The root's value is not equal to the sum of its children)
43+
44+
### Step 2: Approach to Solve the Problem
45+
46+
To determine if a binary tree is a Sum Tree, we can use a recursive approach:
47+
48+
1. **Base Cases**:
49+
- If the current node is `NULL`, it contributes `0` to the sum.
50+
- If the node is a leaf (both children are `NULL`), it is a Sum Tree, and its value is returned as it is.
51+
52+
2. **Recursive Calculation**:
53+
- Recursively calculate the sum for the left and right subtrees.
54+
- Check if the current node's value equals the sum of the left and right children's values.
55+
56+
3. **Return Value**:
57+
- Return a pair containing a boolean indicating if the subtree is a Sum Tree and the total sum of the subtree rooted at the current node.
58+
-
59+
## Problem Solution
60+
```cpp
61+
class Solution {
62+
private:
63+
pair<int, int> fastSumTree(Node* root){
64+
if(root == NULL) return {true, 0};
65+
if(root -> left == NULL && root -> right == NULL) return {true, root->data};
66+
67+
68+
pair<bool, int> leftAns = fastSumTree(root -> left);
69+
pair<bool, int> rightAns = fastSumTree(root -> right);
70+
71+
bool left = leftAns.first;
72+
bool right = rightAns.first;
73+
74+
bool condition = root -> data == leftAns.second + rightAns.second;
75+
76+
pair<int, int> ans;
77+
if(left && right && condition){
78+
ans.first = true;
79+
ans.second = 2 * root -> data;
80+
}else{
81+
ans.first = false;
82+
}
83+
84+
85+
return ans;
86+
}
87+
public:
88+
bool isSumTree(Node* root) {
89+
return fastSumTree(root).first;
90+
}
91+
};
92+
```
93+
94+
## Problem Solution Explanation
95+
96+
Here’s the code with detailed explanations:
97+
98+
```cpp
99+
class Solution {
100+
private:
101+
pair<int, int> fastSumTree(Node* root) {
102+
```
103+
- **Explanation**: This begins the definition of the `fastSumTree` function, which takes a `Node*` as input and returns a pair containing a boolean and an integer.
104+
105+
```cpp
106+
if(root == NULL) return {true, 0};
107+
```
108+
- **Explanation**: If the current node is `NULL`, it is considered a Sum Tree with a sum of `0`. Return a pair `{true, 0}`.
109+
110+
```cpp
111+
if(root -> left == NULL && root -> right == NULL) return {true, root->data};
112+
```
113+
- **Explanation**: If the current node is a leaf node (both children are `NULL`), it is a Sum Tree, and its value is returned. Return `{true, root->data}`.
114+
115+
```cpp
116+
pair<bool, int> leftAns = fastSumTree(root -> left);
117+
pair<bool, int> rightAns = fastSumTree(root -> right);
118+
```
119+
- **Explanation**: Recursively call `fastSumTree` on the left and right children, storing the results in `leftAns` and `rightAns`.
120+
121+
```cpp
122+
bool left = leftAns.first;
123+
bool right = rightAns.first;
124+
```
125+
- **Explanation**: Extract the boolean values from `leftAns` and `rightAns`, which indicate whether the left and right subtrees are Sum Trees.
126+
127+
```cpp
128+
bool condition = root -> data == leftAns.second + rightAns.second;
129+
```
130+
- **Explanation**: Check if the current node's value is equal to the sum of the values of its left and right children (i.e., `leftAns.second` and `rightAns.second`).
131+
132+
```cpp
133+
pair<int, int> ans;
134+
```
135+
- **Explanation**: Declare a pair `ans` to store the result for the current node.
136+
137+
```cpp
138+
if(left && right && condition) {
139+
ans.first = true;
140+
ans.second = 2 * root -> data;
141+
} else {
142+
ans.first = false;
143+
}
144+
```
145+
- **Explanation**:
146+
- If both subtrees are Sum Trees and the condition holds, set `ans.first` to `true`, indicating the current subtree is a Sum Tree, and calculate the sum to return as `2 * root->data` (since the node's value is counted as twice in the sum tree condition).
147+
- Otherwise, set `ans.first` to `false`.
148+
149+
```cpp
150+
return ans;
151+
}
152+
```
153+
- **Explanation**: Return the result for the current node, which contains whether it is a Sum Tree and the sum of the values.
154+
155+
```cpp
156+
public:
157+
bool isSumTree(Node* root) {
158+
return fastSumTree(root).first;
159+
}
160+
};
161+
```
162+
- **Explanation**: The `isSumTree` function is a public method that initiates the check for the whole tree by calling `fastSumTree` and returning the boolean value indicating whether the tree is a Sum Tree.
163+
164+
### Step-by-Step Example Walkthrough
165+
166+
Consider the following tree:
167+
168+
- **Input Tree**:
169+
```
170+
26
171+
/ \
172+
10 3
173+
/ \ \
174+
4 6 3
175+
```
176+
177+
1. **Start at Root (Node 26)**:
178+
- Call `fastSumTree(26)`.
179+
180+
2. **Left Subtree (Node 10)**:
181+
- Call `fastSumTree(10)`.
182+
- Further, call `fastSumTree(4)` (left child).
183+
- Returns `{true, 4}` (leaf node).
184+
- Call `fastSumTree(6)` (right child).
185+
- Returns `{true, 6}` (leaf node).
186+
- Now check if `10 == 4 + 6` (true).
187+
- Return `{true, 20}` for Node 10.
188+
189+
3. **Right Subtree (Node 3)**:
190+
- Call `fastSumTree(3)`.
191+
- Call `fastSumTree(NULL)` (left child).
192+
- Returns `{true, 0}` (NULL).
193+
- Call `fastSumTree(3)` (right child).
194+
- Returns `{true, 3}` (leaf node).
195+
- Now check if `3 == 0 + 3` (true).
196+
- Return `{true, 6}` for Node 3.
197+
198+
4. **Final Check at Root**:
199+
- Now check if `26 == 20 + 6` (true).
200+
- Return `{true, 52}` for Node 26.
201+
202+
The output is `true`, confirming that the tree is a Sum Tree.
203+
204+
### Step 4: Time and Space Complexity
205+
206+
1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the binary tree.
207+
- Each node is visited exactly once to check if the tree is a Sum Tree.
208+
209+
2. **Space Complexity**: **O(H)**, where `H` is the height of the tree.
210+
- The space complexity is primarily due to the recursive call stack. In the worst case (skewed tree), it could be `O(N)`, while for balanced trees, it would be `O(log N)`.
211+
212+
Overall, this approach efficiently determines whether a binary tree is a Sum Tree using recursion.

0 commit comments

Comments
 (0)