From 9e295aacf6e92fabe8c5b1475587b67d6a53d86f Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sat, 24 Aug 2024 18:08:17 +0800 Subject: [PATCH] 195th Commit --- README.md | 3 +- src/page-5/437. Path Sum III/pathSum.test.ts | 17 ++++++++ src/page-5/437. Path Sum III/pathSum.ts | 46 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/page-5/437. Path Sum III/pathSum.test.ts create mode 100644 src/page-5/437. Path Sum III/pathSum.ts diff --git a/README.md b/README.md index a6c4ff9..cde19a3 100644 --- a/README.md +++ b/README.md @@ -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 | | | | ---------------------------------------- | -------- | ------ | diff --git a/src/page-5/437. Path Sum III/pathSum.test.ts b/src/page-5/437. Path Sum III/pathSum.test.ts new file mode 100644 index 0000000..d6458fb --- /dev/null +++ b/src/page-5/437. Path Sum III/pathSum.test.ts @@ -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); + } + }); +}); diff --git a/src/page-5/437. Path Sum III/pathSum.ts b/src/page-5/437. Path Sum III/pathSum.ts new file mode 100644 index 0000000..0ce84a7 --- /dev/null +++ b/src/page-5/437. Path Sum III/pathSum.ts @@ -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; +};