diff --git a/README.md b/README.md index cde19a3..0785686 100644 --- a/README.md +++ b/README.md @@ -175,13 +175,14 @@ Ace Coding Interview with 75 Qs | 872. Leaf-Similar Trees | [Solution][872] | Easy | | 1448. Count Good Nodes in Binary Tree | [Solution][1448] | Medium | | 437. Path Sum III | [Solution][437] | Medium | -| 1372. Longest ZigZag Path in a Binary Tree | Solution | Medium | +| 1372. Longest ZigZag Path in a Binary Tree | [Solution][1372] | 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 +[1372]: ./src/page-13/1372.%20Longest%20ZigZag%20Path%20in%20a%20Binary%20Tree/longestZigZag.ts | Binary Tree - BFS | | | | ---------------------------------------- | -------- | ------ | diff --git a/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.test.ts b/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.test.ts new file mode 100644 index 0000000..150863a --- /dev/null +++ b/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.test.ts @@ -0,0 +1,23 @@ +import { generateBinaryTree } from '~/utils/binary-tree'; + +import { longestZigZag } from './longestZigZag'; + +describe('1372. Longest ZigZag Path in a Binary Tree', () => { + test('longestZigZag', () => { + { + // biome-ignore format: the array should not be formatted + const root = generateBinaryTree([1, null, 1, 1, 1, null, null, 1, 1, null, 1, null, null, null, 1]); + expect(longestZigZag(root)).toBe(3); + } + + { + const root = generateBinaryTree([1, 1, 1, null, 1, null, null, 1, 1, null, 1]); + expect(longestZigZag(root)).toBe(4); + } + + { + const root = generateBinaryTree([1]); + expect(longestZigZag(root)).toBe(0); + } + }); +}); diff --git a/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.ts b/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.ts new file mode 100644 index 0000000..e177951 --- /dev/null +++ b/src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.ts @@ -0,0 +1,36 @@ +import type { TreeNode } from '~/utils/binary-tree'; + +type LongestZigZag = (root: TreeNode | null) => number; + +/** + * Accepted + */ +export const longestZigZag: LongestZigZag = (root) => { + let maxLength = 0; + + function dfs(node: TreeNode | null, direction: 'left' | 'right', length: number): void { + if (!node) return; + + // Update the maximum ZigZag length + maxLength = Math.max(maxLength, length); + + // If the current direction is 'left', try to go left and then right + if (direction === 'left') { + dfs(node.left, 'right', length + 1); + dfs(node.right, 'left', 1); + } + // If the current direction is 'right', try to go right and then left + else { + dfs(node.right, 'left', length + 1); + dfs(node.left, 'right', 1); + } + } + + // Start DFS from the root, considering both initial directions + if (root) { + dfs(root.left, 'right', 1); + dfs(root.right, 'left', 1); + } + + return maxLength; +};