You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ✅ Pattern 08:Tree Depth First Search.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -124,7 +124,7 @@ findPaths(root, 23);
124
124
- We can calculate a tighter time complexity of `O(NlogN)` from the space complexity discussion below.
125
125
- 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).
126
126
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.
128
128
129
129
https://leetcode.com/problems/binary-tree-paths/
130
130
@@ -180,7 +180,7 @@ root.right.right = new TreeNode(5)
180
180
findPaths(root);
181
181
````
182
182
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.
184
184
185
185
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.
186
186
````js
@@ -287,9 +287,9 @@ console.log(`Total Sum of Path Numbers: ${findSumOfPathNumbers(root)}`)
287
287
- 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.
288
288
- 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).
289
289
## 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.
291
291
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.
> 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).
341
341
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:
343
343
1. We will keep track of the current path in a list which will be passed to every recursive call.
344
344
2. Whenever we traverse a node we will do two things:
345
345
346
346
- 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.
348
348
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.
0 commit comments