Skip to content

Commit d7489db

Browse files
committed
update
1 parent 10dc4e1 commit d7489db

3 files changed

+623
-290
lines changed

images/fib4.png

85.3 KB
Loading

✅ Pattern 08:Tree Depth First Search.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ findPaths(root, 23);
124124
- We can calculate a tighter time complexity of `O(NlogN)` from the space complexity discussion below.
125125
- If we ignore the space required for the `allPaths` list, the space complexity of the above algorithm will be `O(N)` in the worst case. This space will be used to store the <b>recursion</b> stack. The worst-case will happen when the given tree is a <i>linked list</i> (i.e., every node has only one child).
126126

127-
> 🌟 Given a binary tree, return all <i>root-to-leaf path</i> s.
127+
> 🌟 Given a binary tree, return all <i>root-to-leaf</i> paths.
128128
129129
https://leetcode.com/problems/binary-tree-paths/
130130

@@ -180,7 +180,7 @@ root.right.right = new TreeNode(5)
180180
findPaths(root);
181181
````
182182

183-
> 🌟 Given a binary tree, find the <i>root-to-leaf path</i> with the maximum sum.
183+
> 🌟 Given a binary tree, find the <i>root-to-leaf</i> path with the maximum sum.
184184
185185
We need to find the path with the maximum sum. As we traverse all paths, we can keep track of the path with the maximum sum.
186186
````js
@@ -287,9 +287,9 @@ console.log(`Total Sum of Path Numbers: ${findSumOfPathNumbers(root)}`)
287287
- The time complexity of the above algorithm is `O(N)`, where `N` is the total number of nodes in the tree. This is due to the fact that we traverse each node once.
288288
- The space complexity of the above algorithm will be `O(N)` in the worst case. This space will be used to store the <b>recursion</b> stack. The worst case will happen when the given tree is a <i>linked list</i> (i.e., every node has only one child).
289289
## Path With Given Sequence (medium)
290-
> Given a binary tree and a number sequence, find if the sequence is present as a <i>root-to-leaf path</i> in the given tree.
290+
> Given a binary tree and a number sequence, find if the sequence is present as a <i>root-to-leaf path</i> in the given tree.
291291
292-
This problem follows the <b>Binary Tree Path Sum</b> pattern. We can follow the same <b>DFS</b> approach and additionally, track the element of the given sequence that we should match with the current node. Also, we can return false as soon as we find a mismatch between the sequence and the node value.
292+
This problem follows the <b>[Binary Tree Path Sum](#binary-tree-path-sum-easy)</b> pattern. We can follow the same <b>DFS</b> approach and additionally, track the element of the given sequence that we should match with the current node. Also, we can return false as soon as we find a mismatch between the sequence and the node value.
293293
````js
294294
class TreeNode {
295295
constructor(value) {
@@ -339,14 +339,14 @@ https://leetcode.com/problems/path-sum-iii/
339339

340340
> Given a binary tree and a number `S`, find all paths in the tree such that the sum of all the node values of each path equals `S`. Please note that the paths can start or end at any node but all paths must follow direction from parent to child (top to bottom).
341341
342-
This problem follows the <b>Binary Tree Path Sum</b> pattern. We can follow the same <b>DFS</b> approach. But there will be four differences:
342+
This problem follows the <b>[Binary Tree Path Sum](#binary-tree-path-sum-easy)</b> pattern. We can follow the same <b>DFS</b> approach. But there will be four differences:
343343
1. We will keep track of the current path in a list which will be passed to every recursive call.
344344
2. Whenever we traverse a node we will do two things:
345345

346346
- Add the current node to the current path.
347-
- As we added a new node to the current path, we should find the sums of all sub-paths ending at the current node. If the sum of any sub-path is equal to S` we will increment our path count.
347+
- As we added a new node to the current path, we should find the sums of all sub-paths ending at the current node. If the sum of any sub-path is equal to `S` we will increment our path count.
348348
3. We will traverse all paths and will not stop processing after finding the first path.
349-
4. Remove the current node from the current path before returning from the function. This is needed to Backtrack while we are going up the recursive call stack to process other paths.
349+
4. Remove the current node from the current path before returning from the function. This is needed to <i>backtrack</i> while we are going up the recursive call stack to process other paths.
350350
````js
351351
class TreeNode {
352352
constructor(value, right = null, left = null) {

0 commit comments

Comments
 (0)