-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e295aa
commit 772e739
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/page-13/1372. Longest ZigZag Path in a Binary Tree/longestZigZag.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |