-
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
ee1e0f3
commit 60e0777
Showing
3 changed files
with
64 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
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,20 @@ | ||
import { generateBinaryTree } from '~/utils/binary-tree'; | ||
|
||
import { leafSimilar } from './leafSimilar'; | ||
|
||
describe('872. Leaf-Similar Trees', () => { | ||
test('leafSimilar', () => { | ||
{ | ||
const root1 = generateBinaryTree([3, 5, 1, 6, 2, 9, 8, null, null, 7, 4]); | ||
// biome-ignore format: the array should not be formatted | ||
const root2 = generateBinaryTree([3, 5, 1, 6, 7, 4, 2, null, null, null, null, null, null, 9, 8]); | ||
expect(leafSimilar(root1, root2)).toBe(true); | ||
} | ||
|
||
{ | ||
const root1 = generateBinaryTree([1, 2, 3]); | ||
const root2 = generateBinaryTree([1, 3, 2]); | ||
expect(leafSimilar(root1, root2)).toBe(false); | ||
} | ||
}); | ||
}); |
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,42 @@ | ||
import type { TreeNode } from '~/utils/binary-tree'; | ||
|
||
type LeafSimilar = (root1: TreeNode | null, root2: TreeNode | null) => boolean; | ||
|
||
/** | ||
* Accepted | ||
*/ | ||
export const leafSimilar: LeafSimilar = (root1, root2) => { | ||
// Helper function to collect all leaf values of a binary tree | ||
const getLeaves = (root: TreeNode | null): number[] => { | ||
const leaves: number[] = []; | ||
|
||
// Depth-First Search (DFS) function to traverse the tree | ||
const dfs = (node: TreeNode | null) => { | ||
// If the node is null, return (do nothing) | ||
if (node === null) return; | ||
|
||
// If the node is a leaf (no left or right children), add its value to the leaves array | ||
if (node.left === null && node.right === null) leaves.push(node.val); | ||
|
||
// Recursively traverse the left subtree | ||
dfs(node.left); | ||
|
||
// Recursively traverse the right subtree | ||
dfs(node.right); | ||
}; | ||
|
||
// Start DFS traversal from the root to find all leaf nodes | ||
dfs(root); | ||
|
||
// Return the collected leaf values | ||
return leaves; | ||
}; | ||
|
||
// Get the leaf value sequences for both trees | ||
const leaves1 = getLeaves(root1); | ||
const leaves2 = getLeaves(root2); | ||
|
||
// Compare the leaf value sequences for equality | ||
// Convert both sequences to strings and check if they are identical | ||
return JSON.stringify(leaves1) === JSON.stringify(leaves2); | ||
}; |