Skip to content

Commit 89a4470

Browse files

File tree

8 files changed

+100
-2
lines changed

8 files changed

+100
-2
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/maximum-binary-tree-ii/
14+
15+
https://leetcode.cn/problems/maximum-binary-tree/
16+
1317
https://leetcode-cn.com/problems/number-of-students-doing-homework-at-a-given-time/
1418

1519
https://leetcode.cn/problems/number-of-digit-one/

maximum-binary-tree-ii/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function insertIntoMaxTree(
4+
root: TreeNode | null,
5+
val: number,
6+
): TreeNode | null {
7+
// 如果root是空节点的话,就以val返回一个新节点
8+
// 如果 val 是整棵树最大的,那么原来的这棵树应该是 val 节点的左子树,因为 val 节点是接在原始数组 a 的最后一个元素
9+
if (root == null || root.val < val) {
10+
return new TreeNode(val, root);
11+
} // 如果 val 不是最大的,那么就应该在右子树上,因为 val 节点是接在原始数组 a 的最后一个元素
12+
else {
13+
root.right = insertIntoMaxTree(root.right, val);
14+
15+
return root;
16+
}
17+
}

maximum-binary-tree-ii/test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { assertEquals } from "../deps.ts";
2+
import { TreeNodeLeetCodeParse } from "../mod.ts";
3+
import constructMaximumBinaryTreeII from "./index.ts";
4+
Deno.test("maximum-binary-tree-ii", () => {
5+
const inputs: Array<[(number | null)[], number]> = [
6+
[[4, 1, 3, null, null, 2], 5],
7+
[[5, 2, 4, null, 1], 3],
8+
[[5, 2, 3, null, 1], 4],
9+
];
10+
const outputs = [
11+
[5, 4, null, 1, 3, null, null, 2],
12+
[5, 2, 4, null, 1, null, 3],
13+
[5, 2, 4, null, 1, 3],
14+
];
15+
assertEquals(
16+
inputs.map(([input, k]) =>
17+
constructMaximumBinaryTreeII(
18+
TreeNodeLeetCodeParse(JSON.stringify(input)),
19+
k,
20+
)
21+
),
22+
outputs.map((a) => TreeNodeLeetCodeParse(JSON.stringify(a))),
23+
);
24+
});

maximum-binary-tree/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
import insertIntoMaxTree from "../maximum-binary-tree-ii/index.ts";
3+
4+
export default function constructMaximumBinaryTree(
5+
nums: number[],
6+
): TreeNode | null {
7+
return nums.reduce(
8+
(a: TreeNode | null, v: number) => insertIntoMaxTree(a, v),
9+
null,
10+
);
11+
}

maximum-binary-tree/test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { assertEquals } from "../deps.ts";
2+
import { TreeNodeLeetCodeStringify } from "../mod.ts";
3+
import constructMaximumBinaryTree from "./index.ts";
4+
5+
Deno.test("maximum-binary-tree", () => {
6+
const inputs = [
7+
[3, 2, 1, 6, 0, 5],
8+
[3, 2, 1],
9+
];
10+
const outputs = [
11+
[6, 3, 5, null, 2, 0, null, null, 1],
12+
[3, null, 2, null, 1],
13+
];
14+
assertEquals(
15+
inputs
16+
.map((input) => constructMaximumBinaryTree(input))
17+
.map((a) => TreeNodeLeetCodeStringify(a)),
18+
outputs.map((a) => JSON.stringify(a)),
19+
);
20+
});

utils/TreeNodeLeetCodeParse.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
22
import { TreeNode as TreeNodeClass } from "../deps.ts";
33
export function TreeNodeLeetCodeParse(s: string): TreeNode | null {
4-
return TreeNodeClass.create(JSON.parse(s));
4+
return treeNodeNew(TreeNodeClass.create(JSON.parse(s)));
5+
}
6+
function treeNodeNew(t: TreeNodeClass | null): TreeNode | null {
7+
if (t == null) {
8+
return null;
9+
}
10+
const n = new TreeNode();
11+
n.val = t.val;
12+
n.left = treeNodeNew(t.left);
13+
n.right = treeNodeNew(t.right);
14+
return n;
515
}

utils/TreeNodeLeetCodeStringify.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
22
import { TreeNode as TreeNodeClass } from "../deps.ts";
33
export function TreeNodeLeetCodeStringify(root: TreeNode | null): string {
4-
return JSON.stringify(TreeNodeClass.show(root));
4+
return JSON.stringify(TreeNodeClass.show(treeNodeCreate(root)));
5+
}
6+
function treeNodeCreate(t: TreeNode | null): TreeNodeClass | null {
7+
if (t == null) {
8+
return null;
9+
}
10+
const n = new TreeNodeClass();
11+
n.val = t.val;
12+
n.left = treeNodeCreate(t.left);
13+
n.right = treeNodeCreate(t.right);
14+
return n;
515
}

utils/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/egregors/TreeNode v1.0.3 h1:oSel21BpUt99TWGrFJ1QbSBBEeQSdwOqOpX9eSqILNE=
33
github.com/egregors/TreeNode v1.0.3/go.mod h1:c2sTJKaqtn/Ju3J7rFi6RvJqbLFAEObRDJP6yqRCdaA=
4+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
45
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
56
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
67
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
8+
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=

0 commit comments

Comments
 (0)