File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Root to leaf path sum - GFG Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Root to leaf path sum
2
+ ## Easy
3
+ <div class =" problem-statement " >
4
+ <p></p><p><span style="font-size:18px">Given a binary tree and an integer S, check whether there is root to leaf path with its sum as S.</span></p>
5
+
6
+ <p ><strong ><span style =" font-size :18px " >Example 1:</span ></strong ></p >
7
+
8
+ <pre ><span style =" font-size :18px " ><strong >Input:</strong >
9
+ Tree =
10
+ 1
11
+ / \
12
+ 2 3
13
+ S = 2</span >
14
+
15
+ <span style =" font-size :18px " ><strong >Output: </strong >0</span >
16
+
17
+ <span style =" font-size :18px " ><strong >Explanation:</strong >
18
+ There is no root to leaf path with sum 2.</span ></pre >
19
+
20
+ <p ><strong ><span style =" font-size :18px " >Example 2:</span ></strong ></p >
21
+
22
+ <pre ><span style =" font-size :18px " ><strong >Input:</strong >
23
+ Tree =
24
+ 1
25
+ / \
26
+ 2 3
27
+ S = 4</span >
28
+
29
+ <span style =" font-size :18px " ><strong >Output:</strong > 1</span >
30
+
31
+ <span style =" font-size :18px " ><strong >Explanation:</strong >
32
+ The sum of path from leaf node 3 to root 1 is 4.</span ></pre >
33
+
34
+ <p ><br >
35
+ <span style =" font-size :18px " ><strong >Your Task:   ; </strong ><br >
36
+ You dont need to read input or print anything. Complete the function<strong > hasPathSum()</strong > which takes <strong >root </strong >node and target sum <strong >S</strong > as input parameter and returns true if path exists otherwise it returns false.</span ></p >
37
+
38
+ <p ><br >
39
+ <span style =" font-size :18px " ><strong >Expected Time Complexity: </strong >O(N)<br >
40
+ <strong >Expected Auxiliary Space:</strong > O(height of tree)</span ></p >
41
+
42
+ <p ><span style =" font-size :18px " ><strong >Constraints:</strong ><br >
43
+ 1 ≤ N ≤ 10^4<br >
44
+ 1 ≤ S ≤ 10^6</span ></p >
45
+ <p ></p >
46
+ </div>
You can’t perform that action at this time.
0 commit comments