Skip to content

Commit

Permalink
193rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Aug 17, 2024
1 parent ee1e0f3 commit 60e0777
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ Ace Coding Interview with 75 Qs
| Binary Tree - DFS | | |
| -------------------------------------------- | --------------- | ------ |
| 104. Maximum Depth of Binary Tree | [Solution][104] | Easy |
| 872. Leaf-Similar Trees | Solution | Easy |
| 872. Leaf-Similar Trees | [Solution][872] | Easy |
| 1448. Count Good Nodes in Binary Tree | Solution | Medium |
| 437. Path Sum III | Solution | 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

| Binary Tree - BFS | | |
| ---------------------------------------- | -------- | ------ |
Expand Down
20 changes: 20 additions & 0 deletions src/page-9/872. Leaf-Similar Trees/leafSimilar.test.ts
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);
}
});
});
42 changes: 42 additions & 0 deletions src/page-9/872. Leaf-Similar Trees/leafSimilar.ts
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);
};

0 comments on commit 60e0777

Please sign in to comment.