Skip to content

Commit 76b0da2

Browse files
committed
added leafSimilar
1 parent 1d94853 commit 76b0da2

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/bst/leaf-similar.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { createBinaryTree, TreeNode } from "./utilities";
2+
3+
/**
4+
* @problem
5+
* [872. Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees)
6+
*/
7+
function leafSimilar(root1: TreeNode, root2: TreeNode): boolean {
8+
const getLeafs = (root: TreeNode): number[] => {
9+
if (!root.left && !root.right) {
10+
return [root.val];
11+
}
12+
const left = root.left ? getLeafs(root.left) : [];
13+
const right = root.right ? getLeafs(root.right) : [];
14+
return [...left, ...right];
15+
}
16+
const root1Leafs = getLeafs(root1).join('#');
17+
const root2Leafs = getLeafs(root2).join('#');
18+
return root1Leafs === root2Leafs;
19+
};
20+
21+
export function leafSimilarDBG() {
22+
const tests = [
23+
{
24+
input: {
25+
root1: createBinaryTree([3, 5, 1, 6, 2, 9, 8, null, null, 7, 4]),
26+
root2: createBinaryTree([3, 5, 1, 6, 7, 4, 2, null, null, null, null, null, null, 9, 8])
27+
},
28+
result: true
29+
},
30+
{
31+
input: {
32+
root1: createBinaryTree([1, 2, 3]),
33+
root2: createBinaryTree([1, 3, 2])
34+
},
35+
result: false
36+
},
37+
{
38+
input: {
39+
root1: createBinaryTree([1]),
40+
root2: createBinaryTree([1])
41+
},
42+
result: true
43+
},
44+
];
45+
46+
tests.forEach((test, index) => {
47+
const output = leafSimilar(test.input.root1, test.input.root2);
48+
const success = output === test.result;
49+
if (success) {
50+
console.log(`${index} success`);
51+
} else {
52+
console.log(`${index} fail`);
53+
console.log(`expected ${test.result}`);
54+
console.log(`got ${output}`);
55+
}
56+
});
57+
}

src/bst/utilities/bst.utilities.ts

+29
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,33 @@ export function printTree(node: TreeNode): void {
3737
_printTree(node.left, childPrefix, false, false);
3838
}
3939
}
40+
}
41+
42+
export function createBinaryTree(values: (number | null)[]): TreeNode | null {
43+
if (values.length === 0 || values[0] == null) return null;
44+
45+
const root = new TreeNode(values[0]);
46+
const queue: (TreeNode | null)[] = [root];
47+
let i = 1;
48+
49+
while (i < values.length) {
50+
const current = queue.shift();
51+
if (!current) continue;
52+
53+
const leftVal = values[i++];
54+
if (leftVal != null) {
55+
current.left = new TreeNode(leftVal);
56+
queue.push(current.left);
57+
}
58+
59+
if (i >= values.length) break;
60+
61+
const rightVal = values[i++];
62+
if (rightVal != null) {
63+
current.right = new TreeNode(rightVal);
64+
queue.push(current.right);
65+
}
66+
}
67+
68+
return root;
4069
}

src/main.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { deleteMiddleDBG } from "./linked_list/delete-middle";
2-
import { oddEvenListDBG } from "./linked_list/odd-even-list";
1+
import { leafSimilarDBG } from "./bst/leaf-similar";
32

4-
oddEvenListDBG()
3+
leafSimilarDBG()

0 commit comments

Comments
 (0)