Skip to content

Commit a8c44ed

Browse files
committed
leetcode
1 parent feae98b commit a8c44ed

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

PathSum/path_sum.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,12 @@ class D {
146146
return false;
147147
}
148148
}
149+
150+
class E {
151+
bool hasPathSum(TreeNode? root, int targetSum) {
152+
return (root?.left ?? root?.right) != null
153+
? hasPathSum(root?.left, targetSum - root!.val) ||
154+
hasPathSum(root.right, targetSum - root.val)
155+
: (root?.val ?? int) == targetSum;
156+
}
157+
}

PathSum/path_sum.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 🔥 Dart 🔥 || 3 solution || Recursive - Iterative - PostOrder traversal
1+
# 🔥 Path Sum 🔥 || 4 solution || Simple Fast and Easy || with Explanation
22

33
## Definition for a Binary Tree Node
44

@@ -18,10 +18,18 @@ class Solution {
1818
// Runtime: 437 ms, faster than 47.06% of Dart online submissions for Path Sum.
1919
// Memory Usage: 143.1 MB, less than 70.59% of Dart online submissions for Path Sum.
2020
bool hasPathSum(TreeNode? root, int targetSum) {
21+
// if the root is null means empty which means we have nothing to begin with
2122
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
2227
if (root.left == null && root.right == null && root.val == targetSum)
2328
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
2431
return hasPathSum(root.left, targetSum - root.val) ||
32+
// same as above but on the right side of the tree
2533
hasPathSum(root.right, targetSum - root.val);
2634
}
2735
}
@@ -37,30 +45,51 @@ class Solution {
3745
// Runtime: 619 ms, faster than 17.65% of Dart online submissions for Path Sum.
3846
// Memory Usage: 145.5 MB, less than 29.41% of Dart online submissions for Path Sum.
3947
bool hasPathSum(TreeNode? root, int targetSum) {
48+
// queue is our stack which hold our value of the tree
4049
Queue<TreeNode> stack = Queue();
50+
// root is null means no value
4151
if (root == null) return false;
52+
// we add the value to the our stack
4253
stack.add(root);
54+
// if the pervious value of the tree is null
4355
TreeNode? pre = null;
4456
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
4661
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
4864
if (pre == null || (pre != top.left && pre != top.right)) {
65+
// we add the value to our target from bottom to top
4966
target += top.val;
67+
// if the value on the right is not null than we will add those to our stack
5068
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
5170
if (top.left != null) stack.add(top.left!);
5271
} else {
72+
// our previous will hold the top value
5373
pre = top;
74+
// and than we will keep removing the last value
5475
stack.removeLast();
76+
// and removing that value from the tree value - bottom to top
5577
target -= top.val;
5678
}
79+
// if the left and right side of the tree are both null
5780
if (top.left == null && top.right == null) {
81+
// and the target sum is the the value we are looking for than congratulation
5882
if (target == targetSum) return true;
83+
// again our previous will be top(from bottom)
84+
// to keep track of the value
5985
pre = top;
86+
// removing the last element from the stack
6087
stack.removeLast();
88+
// removing the target value from the the tree value
6189
target -= top.val;
6290
}
6391
}
92+
// return g false - means found nothing
6493
return false;
6594
}
6695
}
@@ -91,6 +120,19 @@ class Solution {
91120
}
92121
```
93122

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+
94136
## Bonus Solution - Golang - Recursive 100% fast 0ms
95137

96138
```go

0 commit comments

Comments
 (0)