1
- # 🔥 Dart 🔥 || 3 solution || Recursive - Iterative - PostOrder traversal
1
+ # 🔥 Path Sum 🔥 || 4 solution || Simple Fast and Easy || with Explanation
2
2
3
3
## Definition for a Binary Tree Node
4
4
@@ -18,10 +18,18 @@ class Solution {
18
18
// Runtime: 437 ms, faster than 47.06% of Dart online submissions for Path Sum.
19
19
// Memory Usage: 143.1 MB, less than 70.59% of Dart online submissions for Path Sum.
20
20
bool hasPathSum(TreeNode? root, int targetSum) {
21
+ // if the root is null means empty which means we have nothing to begin with
21
22
if (root == null) return false;
23
+ // now if there is nothing on the left side and also on the
24
+ // right side of the tree and the which mean there is only one value
25
+ // at the top of the root and it's the same that we are looking for
26
+ // than congratulations
22
27
if (root.left == null && root.right == null && root.val == targetSum)
23
28
return true;
29
+ // if not than we will walk on the left side of the tree to find the target value
30
+ // by decrementing the value from the root value
24
31
return hasPathSum(root.left, targetSum - root.val) ||
32
+ // same as above but on the right side of the tree
25
33
hasPathSum(root.right, targetSum - root.val);
26
34
}
27
35
}
@@ -37,30 +45,51 @@ class Solution {
37
45
// Runtime: 619 ms, faster than 17.65% of Dart online submissions for Path Sum.
38
46
// Memory Usage: 145.5 MB, less than 29.41% of Dart online submissions for Path Sum.
39
47
bool hasPathSum(TreeNode? root, int targetSum) {
48
+ // queue is our stack which hold our value of the tree
40
49
Queue<TreeNode> stack = Queue();
50
+ // root is null means no value
41
51
if (root == null) return false;
52
+ // we add the value to the our stack
42
53
stack.add(root);
54
+ // if the pervious value of the tree is null
43
55
TreeNode? pre = null;
44
56
int target = 0;
45
- while (!stack.isEmpty) {
57
+ // assuming that our stack is not empty
58
+ while (stack.isNotEmpty) {
59
+ // the last element on the tree from bottom is our first element because
60
+ // we walking from bottom to top
46
61
TreeNode? top = stack.last;
47
-
62
+ // if the previous value is null and previous value is not same on both side
63
+ // of the tree
48
64
if (pre == null || (pre != top.left && pre != top.right)) {
65
+ // we add the value to our target from bottom to top
49
66
target += top.val;
67
+ // if the value on the right is not null than we will add those to our stack
50
68
if (top.right != null) stack.add(top.right!);
69
+ // if the value on the left is not null than we will add those to our stack
51
70
if (top.left != null) stack.add(top.left!);
52
71
} else {
72
+ // our previous will hold the top value
53
73
pre = top;
74
+ // and than we will keep removing the last value
54
75
stack.removeLast();
76
+ // and removing that value from the tree value - bottom to top
55
77
target -= top.val;
56
78
}
79
+ // if the left and right side of the tree are both null
57
80
if (top.left == null && top.right == null) {
81
+ // and the target sum is the the value we are looking for than congratulation
58
82
if (target == targetSum) return true;
83
+ // again our previous will be top(from bottom)
84
+ // to keep track of the value
59
85
pre = top;
86
+ // removing the last element from the stack
60
87
stack.removeLast();
88
+ // removing the target value from the the tree value
61
89
target -= top.val;
62
90
}
63
91
}
92
+ // return g false - means found nothing
64
93
return false;
65
94
}
66
95
}
@@ -91,6 +120,19 @@ class Solution {
91
120
}
92
121
```
93
122
123
+ ## Solution - 4
124
+
125
+ ``` dart
126
+ class E {
127
+ bool hasPathSum(TreeNode? root, int targetSum) {
128
+ return (root?.left ?? root?.right) != null
129
+ ? hasPathSum(root?.left, targetSum - root!.val) ||
130
+ hasPathSum(root.right, targetSum - root.val)
131
+ : (root?.val ?? int) == targetSum;
132
+ }
133
+ }
134
+ ```
135
+
94
136
## Bonus Solution - Golang - Recursive 100% fast 0ms
95
137
96
138
``` go
0 commit comments