Skip to content

Commit

Permalink
195th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 24, 2024
1 parent ec88181 commit 9e295aa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,14 @@ Ace Coding Interview with 75 Qs
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
| 872. Leaf-Similar Trees | [Solution][872] | Easy |
| 1448. Count Good Nodes in Binary Tree | [Solution][1448] | Medium |
| 437. Path Sum III | Solution | Medium |
| 437. Path Sum III | [Solution][437] | Medium |
| 1372. Longest ZigZag Path in a Binary Tree | Solution | Medium |
| 236. Lowest Common Ancestor of a Binary Tree | Solution | Medium |

[104]: ./src/page-2/104.%20Maximum%20Depth%20of%20Binary%20Tree/maxDepth.ts
[872]: ./src/page-9/872.%20Leaf-Similar%20Trees/leafSimilar.ts
[1448]: ./src/page-14/1448.%20Count%20Good%20Nodes%20in%20Binary%20Tree/goodNodes.ts
[437]: ./src/page-5/437.%20Path%20Sum%20III/pathSum.ts

| Binary Tree - BFS | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
17 changes: 17 additions & 0 deletions src/page-5/437. Path Sum III/pathSum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { generateBinaryTree } from '~/utils/binary-tree';

import { pathSum } from './pathSum';

describe('437. Path Sum III', () => {
test('pathSum', () => {
{
const root = generateBinaryTree([10, 5, -3, 3, 2, null, 11, 3, -2, null, 1]);
expect(pathSum(root, 8)).toBe(3);
}

{
const root = generateBinaryTree([5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1]);
expect(pathSum(root, 22)).toBe(3);
}
});
});
46 changes: 46 additions & 0 deletions src/page-5/437. Path Sum III/pathSum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { TreeNode } from '~/utils/binary-tree';

type PathSum = (root: TreeNode | null, targetSum: number) => number;

/**
* Accepted
*/
export const pathSum: PathSum = (root, targetSum) => {
// Initialize a counter to track the number of valid paths
let count = 0;

// Helper function for DFS traversal, takes the current node and the path from root to this node
const dfs = (node: TreeNode | null, currentPath: number[]) => {
// Base case: If the current node is null, return (end of path)
if (node === null) return;

// Add the current node's value to the path
currentPath.push(node.val);

// Variable to track the sum of values in the current path
let pathSum = 0;

// Check all sub-paths ending at the current node
// We do this by iterating backward from the current node to the start of the path
for (let i = currentPath.length - 1; i >= 0; i--) {
pathSum += currentPath[i];

// If the sum of any sub-path equals the targetSum, increment the counter
if (pathSum === targetSum) count += 1;
}

// Recursively call DFS on the left and right children of the current node
dfs(node.left, currentPath);
dfs(node.right, currentPath);

// Backtrack: Remove the current node's value from the path before returning
// This ensures that the path remains accurate for other branches of the tree
currentPath.pop();
};

// Start DFS traversal from the root with an empty path
dfs(root, []);

// Return the total number of paths that sum up to targetSum
return count;
};

0 comments on commit 9e295aa

Please sign in to comment.