Skip to content

Commit

Permalink
196th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 25, 2024
1 parent 9e295aa commit 772e739
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 772e739

Please sign in to comment.