Skip to content

Commit 626b7b0

Browse files

File tree

5 files changed

+81
-39
lines changed

5 files changed

+81
-39
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/binary-tree-pruning/
14+
15+
https://leetcode.cn/problems/pOCWxh/
16+
1317
https://leetcode.cn/problems/word-ladder/
1418

1519
https://leetcode.cn/problems/add-one-row-to-tree/

binary-tree-pruning/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { TreeNode } from "../mod.ts";
2+
3+
export default function pruneTree(root: TreeNode | null): TreeNode | null {
4+
if (!root) return null;
5+
const left = pruneTree(root.left);
6+
const right = pruneTree(root.right);
7+
if (root.val === 0 && !left && !right) return null;
8+
9+
return new TreeNode(root.val, left, right);
10+
}

binary-tree-pruning/test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import pruneTree from "./index.ts";
3+
4+
Deno.test("binary-tree-pruning", () => {
5+
const inputs = [
6+
{
7+
val: 1,
8+
left: {
9+
val: 0,
10+
left: { val: 0, left: null, right: null },
11+
right: { val: 0, left: null, right: null },
12+
},
13+
right: {
14+
val: 1,
15+
left: { val: 0, left: null, right: null },
16+
right: { val: 1, left: null, right: null },
17+
},
18+
},
19+
{
20+
val: 1,
21+
left: {
22+
val: 1,
23+
left: {
24+
val: 1,
25+
left: { val: 0, left: null, right: null },
26+
right: null,
27+
},
28+
right: { val: 1, left: null, right: null },
29+
},
30+
right: {
31+
val: 0,
32+
left: { val: 0, left: null, right: null },
33+
right: { val: 1, left: null, right: null },
34+
},
35+
},
36+
];
37+
const outputs = [
38+
{
39+
val: 1,
40+
left: null,
41+
right: {
42+
val: 1,
43+
left: null,
44+
right: { val: 1, left: null, right: null },
45+
},
46+
},
47+
{
48+
val: 1,
49+
left: {
50+
val: 1,
51+
left: { val: 1, left: null, right: null },
52+
right: { val: 1, left: null, right: null },
53+
},
54+
right: {
55+
val: 0,
56+
left: null,
57+
right: { val: 1, left: null, right: null },
58+
},
59+
},
60+
];
61+
62+
assertEquals(
63+
structuredClone(inputs.map((input) => pruneTree(input))),
64+
outputs,
65+
);
66+
});

pOCWxh/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "../binary-tree-pruning/index.ts";

serialize-and-deserialize-n-ary-tree/index.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,3 @@ function ArrayToNAryNode(array: (number[] | null)[][]): Node | null {
7373
}
7474
return root;
7575
}
76-
// Deno.test("NAryNodeToArray", () => {
77-
// const root = new Node(1, [
78-
// new Node(3, [new Node(5), new Node(6)]),
79-
// new Node(2),
80-
// new Node(4, [
81-
// new Node(13, [
82-
// new Node(15),
83-
// new Node(16, [
84-
// new Node(43, [
85-
// new Node(25),
86-
// new Node(26, [
87-
// new Node(14, [
88-
// new Node(23, [
89-
// new Node(45),
90-
// new Node(46, [
91-
// new Node(33, [
92-
// new Node(35),
93-
// new Node(36),
94-
// ]),
95-
// ]),
96-
// ]),
97-
// ]),
98-
// ]),
99-
// ]),
100-
// ]),
101-
// ]),
102-
// ]),
103-
// ]);
104-
// console.log(JSON.stringify(root, null, 4), NAryNodeToArray(root));
105-
// console.log(null, NAryNodeToArray(null));
106-
// const root2 = new Node(10);
107-
// console.log(root2, NAryNodeToArray(root2));
108-
// const root3 = new Node(10, [new Node(20, [new Node(200)]), new Node(21)]);
109-
// console.log(JSON.stringify(root3, null, 4), NAryNodeToArray(root3));
110-
// });
111-
// Deno.test("ArrayToNAryNode", () => {
112-
// const array = [[[10]], [[20, 21, 19]], [null, [200]]];
113-
// console.log(array, JSON.stringify(ArrayToNAryNode(array), null, 4));
114-
// });

0 commit comments

Comments
 (0)