Skip to content

Commit 6ce34f2

Browse files
authored
Create README.md
1 parent d5a49e4 commit 6ce34f2

File tree

1 file changed

+196
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/12 - Path Sum

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<h1 align='center'>Path - Sum</h1>
2+
3+
## Problem Statement
4+
5+
**Problem URL :** [Path Sum](https://leetcode.com/problems/path-sum/)
6+
7+
![image](https://github.com/user-attachments/assets/d5296fe5-59e9-42b2-bdb3-a955ea3dacb1)
8+
![image](https://github.com/user-attachments/assets/8d85cd64-8ad2-420a-9740-083278dc0023)
9+
10+
11+
## Problem Explanation
12+
The problem requires us to determine if a given binary tree has a root-to-leaf path such that the sum of the values of the nodes along the path equals a specified target sum. A root-to-leaf path is defined as a path starting from the root node and ending at any leaf node (a node with no children).
13+
14+
#### Examples:
15+
16+
1. **Example 1**:
17+
- **Input**:
18+
- **Tree**:
19+
```
20+
5
21+
/ \
22+
4 8
23+
/ / \
24+
11 13 4
25+
/ \ \
26+
7 2 1
27+
```
28+
- **Target Sum**: `22`
29+
- **Output**: `true` (The path 5 → 4 → 11 → 2 has a sum of `22`.)
30+
31+
2. **Example 2**:
32+
- **Input**:
33+
- **Tree**:
34+
```
35+
1
36+
/ \
37+
2 3
38+
```
39+
- **Target Sum**: `5`
40+
- **Output**: `false` (There is no path that adds up to `5`.)
41+
42+
3. **Example 3**:
43+
- **Input**:
44+
- **Tree**:
45+
```
46+
1
47+
/
48+
2
49+
```
50+
- **Target Sum**: `1`
51+
- **Output**: `false` (The only path is 1 → 2, which sums to `3`, not `1`.)
52+
53+
### Step 2: Approach to Solve the Problem
54+
55+
To solve the problem, we can use a recursive depth-first search (DFS) approach:
56+
57+
1. **Base Case**:
58+
- If the current node is `NULL`, return `false` (no path exists).
59+
60+
2. **Update the Target**:
61+
- Subtract the value of the current node from the `targetSum`.
62+
63+
3. **Leaf Node Check**:
64+
- If the current node is a leaf (both left and right children are `NULL`), check if the updated `targetSum` is `0`. If yes, return `true` (indicating a valid path).
65+
66+
4. **Recursive Calls**:
67+
- Recursively check the left and right subtrees with the updated `targetSum`. If either subtree returns `true`, then a valid path exists.
68+
69+
## Problem Solution
70+
```cpp
71+
/**
72+
* Definition for a binary tree node.
73+
* struct TreeNode {
74+
* int val;
75+
* TreeNode *left;
76+
* TreeNode *right;
77+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
78+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
79+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
80+
* };
81+
*/
82+
class Solution {
83+
public:
84+
bool hasPathSum(TreeNode* root, int targetSum) {
85+
if(root == NULL) return false;
86+
87+
targetSum -= root -> val;
88+
89+
if(root -> left == NULL && root -> right == NULL){
90+
return targetSum == 0;
91+
}
92+
93+
return hasPathSum(root -> left, targetSum) || hasPathSum(root -> right, targetSum);
94+
}
95+
};
96+
```
97+
98+
## Problem Solution Explanation
99+
Here’s the code with detailed explanations:
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool hasPathSum(TreeNode* root, int targetSum) {
105+
```
106+
- **Explanation**: The `hasPathSum` function begins with a `TreeNode* root` representing the root of the binary tree and an `int targetSum` representing the target sum we want to check.
107+
108+
```cpp
109+
if(root == NULL) return false;
110+
```
111+
- **Explanation**: If the current node (`root`) is `NULL`, there is no path to check, so return `false`.
112+
113+
```cpp
114+
targetSum -= root -> val;
115+
```
116+
- **Explanation**: Subtract the value of the current node from `targetSum`. This updates the remaining sum we need to find along the path.
117+
118+
```cpp
119+
if(root -> left == NULL && root -> right == NULL){
120+
return targetSum == 0;
121+
}
122+
```
123+
- **Explanation**: Check if the current node is a leaf node (both children are `NULL`). If it is, return `true` if the `targetSum` has been reduced to `0`, indicating that the path sum equals the original target sum.
124+
125+
```cpp
126+
return hasPathSum(root -> left, targetSum) || hasPathSum(root -> right, targetSum);
127+
}
128+
};
129+
```
130+
- **Explanation**: Recursively call `hasPathSum` on the left and right children of the current node. Use the logical OR (`||`) to return `true` if either subtree contains a path that sums to the required target.
131+
132+
### Step-by-Step Example Walkthrough
133+
134+
Consider the following tree and target sum:
135+
136+
- **Input Tree**:
137+
```
138+
5
139+
/ \
140+
4 8
141+
/ / \
142+
11 13 4
143+
/ \ \
144+
7 2 1
145+
```
146+
- **Target Sum**: `22`
147+
148+
1. **Start at Root (Node 5)**:
149+
- Call `hasPathSum(5, 22)`.
150+
- Update `targetSum` to `22 - 5 = 17`.
151+
152+
2. **Left Subtree (Node 4)**:
153+
- Call `hasPathSum(4, 17)`.
154+
- Update `targetSum` to `17 - 4 = 13`.
155+
156+
3. **Left Subtree of 4 (Node 11)**:
157+
- Call `hasPathSum(11, 13)`.
158+
- Update `targetSum` to `13 - 11 = 2`.
159+
160+
4. **Left Subtree of 11 (Node 7)**:
161+
- Call `hasPathSum(7, 2)`.
162+
- Both children are `NULL`. Return `false` (2 != 0).
163+
164+
5. **Right Subtree of 11 (Node 2)**:
165+
- Call `hasPathSum(2, 2)`.
166+
- Both children are `NULL`. Return `true` (2 == 0).
167+
168+
6. **Back to Node 4**: Since we found a valid path in the left subtree, we return `true`.
169+
170+
7. **Check Right Subtree (Node 8)**:
171+
- Call `hasPathSum(8, 17)`.
172+
- Update `targetSum` to `17 - 8 = 9`.
173+
174+
8. **Left Subtree of 8 (Node 13)**:
175+
- Call `hasPathSum(13, 9)`.
176+
- Both children are `NULL`. Return `false` (9 != 0).
177+
178+
9. **Right Subtree of 8 (Node 4)**:
179+
- Call `hasPathSum(4, 9)`.
180+
- Update `targetSum` to `9 - 4 = 5`.
181+
182+
10. **Right Subtree of 4 (Node 1)**:
183+
- Call `hasPathSum(1, 5)`.
184+
- Both children are `NULL`. Return `false` (5 != 0).
185+
186+
The final output is `true`, as we found the path `5 → 4 → 11 → 2`.
187+
188+
### Step 4: Time and Space Complexity
189+
190+
1. **Time Complexity**: **O(N)**, where `N` is the number of nodes in the binary tree.
191+
- Each node is visited once to check for the path sum.
192+
193+
2. **Space Complexity**: **O(H)**, where `H` is the height of the tree.
194+
- 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)`.
195+
196+
Overall, this approach efficiently checks if there exists a path in the binary tree whose sum equals the specified target sum using recursion.

0 commit comments

Comments
 (0)