Skip to content

Commit 037d6d4

Browse files
committedFeb 3, 2024
chore: 重构树和链表的类,parse,toArray改为静态方法
1 parent f6e5b2a commit 037d6d4

File tree

3 files changed

+43
-57
lines changed

3 files changed

+43
-57
lines changed
 

Diff for: ‎common/structures/Node.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ class Node {
33
this.val = val === undefined ? 0 : val;
44
this.neighbors = neighbors === undefined ? [] : neighbors;
55
}
6-
parse(edges) {
6+
7+
static parse(edges) {
78
const nodeMap = new Map();
8-
9+
910
// 创建节点
1011
const getNode = (val) => {
1112
if (!nodeMap.has(val)) {
@@ -14,45 +15,43 @@ class Node {
1415
}
1516
return nodeMap.get(val);
1617
};
17-
18+
1819
// 连接节点
19-
edges.forEach((neighbors,index) => {
20-
let val = index + 1;
20+
edges.forEach((neighbors, index) => {
21+
const val = index + 1;
2122
const currentNode = getNode(val);
22-
neighbors.forEach(neighborVal => {
23+
neighbors.forEach((neighborVal) => {
2324
const neighborNode = getNode(neighborVal);
2425
currentNode.neighbors.push(neighborNode);
2526
});
2627
});
27-
28+
2829
return nodeMap.size > 0 ? nodeMap.values().next().value : null;
2930
}
30-
toArray(node) {
31+
32+
static toArray(node) {
3133
if (!node) {
3234
return [];
3335
}
34-
36+
3537
const visited = new Set();
3638
const result = [];
37-
39+
3840
const dfs = (currentNode) => {
3941
if (visited.has(currentNode.val)) {
4042
return;
4143
}
42-
const {neighbors, val} = currentNode
44+
const { neighbors, val } = currentNode;
4345
visited.add(val);
44-
result.push(neighbors.map(res=> res.val));
45-
46-
for (const neighbor of currentNode.neighbors) {
46+
result.push(neighbors.map(({ val }) => val));
47+
currentNode.neighbors.forEach((neighbor) => {
4748
dfs(neighbor);
48-
}
49+
});
4950
};
50-
51+
5152
dfs(node);
52-
53+
5354
return result;
5455
}
55-
56-
5756
}
58-
module.exports = Node;
57+
module.exports = Node;

Diff for: ‎common/structures/TreeNode.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
class TreeNode {
22
constructor(val, left, right) {
33
this.left = null;
4-
this.val = (val===undefined ? 0 : val)
5-
this.left = (left===undefined ? null : left)
6-
this.right = (right===undefined ? null : right)
4+
this.val = (val === undefined ? 0 : val);
5+
this.left = (left === undefined ? null : left);
6+
this.right = (right === undefined ? null : right);
77
}
8-
parse(arr) {
9-
if(arr.length === 0) return null
10-
let root = new TreeNode(arr[0])
11-
let queue = [root]
12-
for(let i = 1; i < arr.length; i += 2)
13-
{
14-
let node = queue.shift()
15-
if(arr[i] !== null)
16-
{
17-
node.left = new TreeNode(arr[i])
18-
queue.push(node.left)
8+
9+
static parse(arr) {
10+
if (arr.length === 0) return null;
11+
const root = new TreeNode(arr[0]);
12+
const queue = [root];
13+
for (let i = 1; i < arr.length; i += 2) {
14+
const node = queue.shift();
15+
if (arr[i] !== null) {
16+
node.left = new TreeNode(arr[i]);
17+
queue.push(node.left);
1918
}
20-
if(arr[i+1] !== null)
21-
{
22-
node.right = new TreeNode(arr[i+1])
23-
queue.push(node.right)
19+
if (arr[i + 1] !== null) {
20+
node.right = new TreeNode(arr[i + 1]);
21+
queue.push(node.right);
2422
}
2523
}
26-
return root
24+
return root;
2725
}
28-
toArray(treeNode) {
26+
27+
static toArray(treeNode) {
2928
const result = [];
3029
if (!treeNode) {
3130
return result;
@@ -51,4 +50,4 @@ class TreeNode {
5150
return result;
5251
}
5352
}
54-
module.exports = TreeNode
53+
module.exports = TreeNode;

Diff for: ‎common/utils/parseStructure.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,17 @@ const paramMap = {
77
cases: {
88
ListNode: (_param) => parse(_param),
99
'ListNode[]': (param) => param.map((res) => parse(res)),
10-
TreeNode: (param) => {
11-
const node = new TreeNode(param);
12-
return node.parse(param);
13-
},
14-
Node: (param) => {
15-
const node = new Node(param);
16-
return node.parse(param);
17-
},
10+
TreeNode: (param) => TreeNode.parse(param),
11+
Node: (param) => Node.parse(param),
1812
default: (param) => param,
1913

2014
},
2115
// 返回值map
2216
return: {
2317
ListNode: (param) => toArray(param),
2418
'ListNode[]': (param) => param.map((res) => toArray(res)),
25-
TreeNode: (param) => {
26-
const node = new TreeNode(param);
27-
return node.toArray(param);
28-
},
29-
Node: (param) => {
30-
const node = new Node(param);
31-
return node.toArray(param);
32-
},
19+
TreeNode: (param) => TreeNode.toArray(param),
20+
Node: (param) => Node.toArray(param),
3321
default: (param) => param,
3422

3523
},

0 commit comments

Comments
 (0)
Please sign in to comment.